Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Making Player Invincible


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.player.body.velocity.y = -300;
			this.player.body.velocity.x = 0;
			this.game.time.events.add(Phaser.Timer.SECOND * 1, this.killEnemy, this);
		else if (!this.invincibility){
			this.player.body.velocity.y = -400;
			this.player.body.velocity.x = 0;
			this.game.time.events.add(1000, this.gameOver, this);
	killEnemy: function() {

//code for collecting star 
hitStar: function(player, star) {

//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


  • Recently Browsing   0 members

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