dyarzik Posted February 8, 2014 Share Posted February 8, 2014 Hi guys, I am trying make simple game with Phaser but I am beginner in game developement and I need some help. I want to make game "rabbit hunter " like this Rabbit will spawn, you have to smash him a take some score I have already done, random spawning of rabbit on one of these black dots, score counter, and killing rabbit on click. But I dont know how to spawn rabbit for just 2 sec and then automaticly disapear it if player doesnt "kill" him, and ofcourse som delay between spawns. Can someone me help with this ? PS: sorry for my bad english i hope you can understand me Link to comment Share on other sites More sharing options...
chazzlabs Posted February 9, 2014 Share Posted February 9, 2014 Take a look at this example: http://examples.phaser.io/_site/view_full.html?d=time&f=basic+timed+event.js&t=basic%20timed%20event The important line is this:game.time.events.add(Phaser.Timer.SECOND * 4, fadePicture, this);This creates an event that will trigger after 4 seconds, at which point it will called the fadePicture() function. Your code might look something like this:game.time.events.add(Phaser.Timer.SECOND * 2, removeRabbit, this):Perhaps this might be a suitable solution. I'm not sure if this is the most efficient way to handle what you're asking, so hopefully someone else can correct me if it's not, however, this should be a good starting point. Take a look at the rest of the timing examples for some more insight: http://examples.phaser.io/ Scroll down or search for "Time". Link to comment Share on other sites More sharing options...
dyarzik Posted February 9, 2014 Author Share Posted February 9, 2014 I tried it already but it doesnt work. Look at my code maybe i do something wrong https://gist.github.com/dyarzik/8896969And second problem is that game sometimes freeze, for examle after it spawns some rabbits its stop spawning Link to comment Share on other sites More sharing options...
jpdev Posted February 9, 2014 Share Posted February 9, 2014 If the game freezes always check the error console of the browser. (F12 in most browsers)Most of the times this will point you to the problem. Link to comment Share on other sites More sharing options...
dyarzik Posted February 9, 2014 Author Share Posted February 9, 2014 Thank you for tip, i solved freeze problems But I cant still correctly implement timed event for automatic rabbiit kill Link to comment Share on other sites More sharing options...
dyarzik Posted February 10, 2014 Author Share Posted February 10, 2014 Can someone help me with this error ?Uncaught TypeError: Cannot call method 'destroy' of undefined index.html:61removeRabbit index.html:61Phaser.Timer.update phaser.js:31260Phaser.Time.update phaser.js:30841Phaser.Game.update phaser.js:13468Phaser.RequestAnimationFrame.updateRAF phaser.js:25516_onLoop phaser.js:25501 This happens when i use this function: timer = game.time.create(false); timer.add(2000, removeRabbit, this); timer.start();Source code: https://gist.github.com/dyarzik/8896969 Link to comment Share on other sites More sharing options...
jpdev Posted February 10, 2014 Share Posted February 10, 2014 hi dyarzik, I got it to work, see here: http://janpeter.net/perma/phaser/rabbit.html (I used your screenshot above as artwork, so it will not match you bg.png / rabbit picture).<!doctype html><html> <head> <meta charset="UTF-8" /> <title>Rabbit</title> <script src="lib/phaser.js"></script> </head> <body> Example for <a href=""> </a> <script type="text/javascript"> window.onload = function() { var game = new Phaser.Game(320, 480, Phaser.AUTO, '', { preload: preload, create: create, update: update }); var score = 0; var scoreText; var timer; function preload () { game.load.image('bg', 'bg.png'); game.load.image('rabbit', 'rabbit.png'); } function create () { game.add.sprite(0, 0, 'bg'); scoreText = game.add.text(16, 16, 'Score: 0', { font: '24px arial', fill: '#00FF00' }); game.time.events.loop( Phaser.Timer.SECOND *2, createRabbit, this); timer = game.time.create(false); } function update() { } function destoyIt () { //remove the rabbit, and it's timer (which otherwhise will trigger without context) this.timerInstance.destroy(); this.destroy(); //rabbit is given as context parameter, so "this" is the rabbit-sprite score++; scoreText.content = 'Score: ' + score; } function removeRabbit() { this.destroy(); //rabbit is given as context parameter, so "this" is the rabbit-sprite } //create Rabbit on random position function createRabbit() { //generate the x,y coodinates, to make the code shorter var x = game.rnd.integerInRange(0, 3); var y = game.rnd.integerInRange(0, 3); x = x * 81; y = y * 80 + 30; rabbit = game.add.sprite(x, y, 'rabbit'); rabbit.inputEnabled = true; rabbit.events.onInputUp.add(destoyIt, rabbit); //destroy rabbits on cursor up timer = game.time.create(false); timer.add(2000, removeRabbit, rabbit); rabbit.timerInstance = timer; //give the rabbit it's timer as a reference, to destroy it timer.start(); } }; </script> </body></html>I hope this is helpfull. Explanation why I kill the rabbit on inputUp (instead of down):If you destroy the sprite in the input-callback for input-down, then phaser runs into an error when it wants to call the input-up event on the already destroyed sprite. If you really need to kill the rabbits on input-down, you will need to just "kill" the sprites in the input event, and then reuse them for new rabbits. (Instead of create/destroy you will use one create, and then kill() and reset() and possibly use a group to manage all your rabbits. (see the example game invader for this)) dyarzik 1 Link to comment Share on other sites More sharing options...
dyarzik Posted February 10, 2014 Author Share Posted February 10, 2014 Oh thank you very much, u helped me a lot Link to comment Share on other sites More sharing options...
jvd Posted February 16, 2014 Share Posted February 16, 2014 I would like to use this game on an Android or iPhone device. But unfortunately when I try to open the webpage the game isn't visible.Also on my normal computer with Windows 8.1 and IE11 the page doesn't work. Does someone an idea how to solve this issue? Link to comment Share on other sites More sharing options...
Recommended Posts