Hello Phaser Friends,
I am making a level where a player (called Serol) interacts with obstacles. If Serol jumps on top of the obstacle, he jumps again, avoiding damage. If he collides with an obstacle head on, he loses a life and becomes invisible briefly (to avoid insta-death).
I made a function within update()
which deals with the collision and made Serol invincible upon sideways collision. This made him invincible permanently, so I added a tween to make him able to take damage. This made insta-death come back again. I believe I’m not putting the tween in the right part of the code, so could you please take a minute to examine this code?
overlapping = false;
class Level2 extends Phaser.Scene {
constructor() {
super("level2");
}
pipePositions = [100, 200, 300, 400, 500, 600, 700, 800];
//queue = [];
choices = ['tetromino', 'junk'];
score = 0;
lives = 3;
timeLeft;
minutes;
seconds;
create() {
endgame = false;
this.score = 0;
this.lives = 3;
this.runSpeed = 1.5;
this.maxSpeed = 9.2;
//obstacles
this.obstacle = new Obstacle(this,1000, 520,'obstacle',Math.round(Math.random() * 4)).setOrigin(0.5,1);
this.physics.world.enable(this.obstacle);
this.obstacle.body.immovable = true;
//spawn Serol
this.serol = new Serol(this, 100, 350);
this.physics.add.existing(this.serol);
this.serol.body.setGravityY(3000);
this.serol.anims.play('walkRight',true);
this.serol.setCollideWorldBounds(true);
}
update() {
this.movePlayerManager();
this.updateTimer();
this.sky.tilePositionX += 0.2;
this.mountains.tilePositionX += this.runSpeed;
this.itemMove(this.telescope,this.runSpeed);
this.itemMove(this.obstacle,this.runSpeed);
// Treat 'embedded' as 'touching' also
if (this.telescope.body.embedded) {
this.telescope.body.touching.none = false
};
var touching = !this.telescope.body.touching.none;
var wasTouching = !this.telescope.body.wasTouching.none;
if (touching && !wasTouching) this.telescope.emit("overlapstart");
else if (!touching && wasTouching) this.telescope.emit("overlapend");
// handling collision between obstacle and serol
if (this.serol.alpha == 1){
this.physics.world.collide(this.serol, this.obstacle, function(serol, obstacle){
//obstacle is touching up and serol is touching down
if(obstacle.body.touching.up && serol.body.touching.down){
// in this case just jump again
this.serol.body.velocity.y = -gameSettings.playerYSpeed;
}
else{
//TODO: make Serol ivincible for a bit
serol.alpha = 0.5
var tween = this.tweens.add({
targets: serol,
ease:'Power1',
duration:1500,
repeat:0,
onComplete: function(){
serol.alpha = 1;
},
callbackScope: this
});
// any other way to collide with an obstacle will cause life loss
if ( this.lives <= 1){
this.lives = 0;
//endgame sequence
endgame=true;
//stop timer
this.timedEvent.paused = true;
//remove delayed events
//remove object sprites
this.runSpeed = 0;
//put Serol to sleep
this.serol.anims.play('sleeping',true);
this.transition = this.time.delayedCall(4000, function(){this.scene.start('gameOver')}, [], this); // delay in ms
}else{
this.lives--;
}
this.livesLabel.text = "LIVES " + this.lives;
//update lives gauge
this.lifeGauge.updateLife(this.lives);
}
}, null, this);
}
Thank you!