Sign in to follow this  
dandorf

invincible for a while when you hit

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

Share this post


Link to post
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;}

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.