dandorf Posted October 23, 2014 Share Posted October 23, 2014 like do to when you hit the enemy, this is around for a while invincible? invincible maybe 2 seconds (blinking, for example), and then he can hit again. I have to use some kind of timmer, the game.now ...? Link to comment Share on other sites More sharing options...
spencerTL Posted October 23, 2014 Share Posted October 23, 2014 the phaser sprites have a property called lifespan which after it times out kills the sprite. When I needed the functionality you describe I extended the sprite with a function I basically copied from Phaser's source for lifespan except when the timer expired a flag was changed to allow the sprite to be vulnerable again.Look at the source for sprite and search lifespan to see how it works and then modify it to suit yourself in an extended sprite.http://docs.phaser.io/Sprite.js.html#sunlight-1-line-121 Link to comment Share on other sites More sharing options...
OttoRobba Posted October 23, 2014 Share Posted October 23, 2014 What I would personally do is write two functions, like so:onHit: function(damage) { if (!player.invincible) { //We only damage the player if not invincible player.health -= damage; //we toggle invincibility this.toggleInvincible(); //and then we add a timer to restore the player to a vulnerable state game.time.events.add(2000, this.toggleInvincible, this); }}toggleInvincible: function() { player.invincible = !player.invincible;} valueerror 1 Link to comment Share on other sites More sharing options...
valueerror Posted October 24, 2014 Share Posted October 24, 2014 thats how i did it to.. and in the playerhit function i first ask if theplayer is invincible or not.. thats all you need.. i let the players alpha flash a little bit for that time to make the invincible phase visible to the user...j Link to comment Share on other sites More sharing options...
dandorf Posted October 24, 2014 Author Share Posted October 24, 2014 What I would personally do is write two functions, like so:onHit: function(damage) { if (!player.invincible) { //We only damage the player if not invincible player.health -= damage; //we toggle invincibility this.toggleInvincible(); //and then we add a timer to restore the player to a vulnerable state game.time.events.add(2000, this.toggleInvincible, this); }}toggleInvincible : function() { player.invincible = !player.invincible;} I will use this method to do it, but I have a problem. Each enemy has their own health, created a local variable on your sprite. So when I call the function toggleInvincible have to pass as parameter that enemy ... I do not know how. I have the following: HitOn : function(bullet, enemy){ if (enemy.invincible == false) { enemy.health -= 1; bullet.kill(); if (enemy.health == 0) { enemy.killed = true; //a variable create with me enemy.kill(); } else { enemy.invincible = true; //and then we add a timer to restore the player to a vulnerable state game.time.events.add(2000, this.toggleInvincible(enemy), this); } } }, toggleInvincible: function(enemy) { enemy.invincible = false; //Take error... }, Obtain this error: Uncaught TypeError: Cannot read property 'apply' of undefined phaser.js:40112b.Timer.update phaser.js:40112b.Time.update me.forceSetTimeOut._onLoop phaser.js:35456 Link to comment Share on other sites More sharing options...
OttoRobba Posted October 24, 2014 Share Posted October 24, 2014 Ah, you want the enemy to be invincible, my bad. I thought you meant the player. You can pass a fourth argument for the timer function, which is what the callback will receive. So this: game.time.events.add(2000, this.toggleInvincible, this, enemy); Should fix it Link to comment Share on other sites More sharing options...
dandorf Posted October 25, 2014 Author Share Posted October 25, 2014 That's what I was missing !! Your system works perfectly. Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts