Jump to content

Making Player Invincible


Kraken
 Share

Recommended Posts


I'm trying to make a player invincible for a few seconds and change the players texture during that time frame after collecting an item. Once the 5 seconds is up the player will revert back to original texture and lose invincibility. I have figured out how to change the texture and I'm using a timer to toggle between invisibility. I'm having trouble with the player being killed after collecting the item it seems the player still gets killed after collecting the item. 

 

//code for contacting an enemy
hitEnemy: function(player, slime) {
        if(slime.body.touching.up ){
			this.hitEnemySound.play();
			this.player.body.velocity.y = -300;
			this.player.body.velocity.x = 0;
			slime.play('slimeDead');
			this.game.time.events.add(Phaser.Timer.SECOND * 1, this.killEnemy, this);
			
			
		}
		else if (!this.invincibility){
			this.hitSound.play();
			this.player.loadTexture('hurt');
			this.player.body.velocity.y = -400;
			this.player.body.velocity.x = 0;
			
			this.game.time.events.add(1000, this.gameOver, this);
			}
    },
	killEnemy: function() {
			this.slime.kill();
			
			
		
    },

//code for collecting star 
hitStar: function(player, star) {
			this.hitStarSound.play();
			this.playerInvincible();
			star.kill();
		
    },

//code for player invincibility
	playerInvincible: function() {
		this.invincibility = true;
		if (this.player.key === 'player1'){
        	this.player.loadTexture('player2', 0, false);
			this.game.time.events.add(20000, this.toggleInvincible, this);
			
		}
	},
	toggleInvincible: function(){
		this.invincibility = false;
		if (!this.invinciblity){
			this.player.loadTexture('player1', 0, false);
		}
	},

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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