Jump to content

invincible for a while when you hit


dandorf
 Share

Recommended Posts

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

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;}
Link to comment
Share on other sites

 

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...