How to start Game Over screen?

How do I start the Game over scene when the ball falls into the void?

Game demo

//-------------------------------- CODE --------------------------------------//

var cursors;
var player;
var score = 0;
var scoreText;
var plataforms;
class SceneB extends Phaser.Scene { constructor() { super({key: "SceneB", active: true});}

preload(){
//----------------------pleyer-------------------//
this.load.image('ball', 'assets/Ball.png');
//----------------------Background-------------------//
this.load.image('background', 'assets/img/lofi-1.jpg');
//----------------------Audio-------------------//
this.load.audio('MusicL-1','assets/audio/level-1.mp3');
//----------------------plataforms-------------------//
this.load.image('plataform','assets/plataform.png');
}
create(){
var audio = this.sound.add('MusicL-1',{loop:true}); audio.play();
//-----------------backgraund-------------//
this.add.image(400, 300, 'background').setDisplaySize(800, 600);
//-------------------- Music -------------------------------------//
var audio = this.sound.add('MusicL-1',{loop:true}); audio.play();
//-------------------- Score -------------------------------------//
scoreText=this.add.text(16, 16, 'SCORE '+ score,{ fontSize:'32px', fill:'#8357eb',fontFamily: 'verdana, arial, sans-serif'});
//--------------------  Ball phisics -------------------------------------//
player = this.physics.add.sprite(Phaser.Math.Between(0,600), 0, 'ball');
player.setDisplaySize(50,50);
player.setGravityY(14000);
player.body.setCollideWorldBounds(true);
//------------------ Create plataforms --------------//
timedEvent = this.time.addEvent({ delay: 1000, callback: createPlataforms, callbackScope: this, loop: true });
function createPlataforms(){
plataforms = this.physics.add.sprite(Phaser.Math.Between(0,800), 0, 'plataform');
plataforms.body.immovable = true;
plataforms.setDisplaySize(200, 200);
plataforms.setVelocityY(100) 
plataforms.setSize(80,8);
//--------------------- coliders-------------------------//
this.physics.add.collider(player,plataforms);
plataforms.body.checkCollision.up = true;
plataforms.body.checkCollision.down = false;
plataforms.body.checkCollision.left = false;
plataforms.body.checkCollision.right = false;
this.physics.world.setBoundsCollision(true,true,true, false);
}//-------------------create controls----------//
cursors = this.input.keyboard.createCursorKeys();
LeftButton=this.add.text(16, 480, ' ◄ ',{ fontSize:'64px', fill:'#8357eb',fontFamily: 'verdana, arial, sans-serif'}).setInteractive();
RightButton=this.add.text(128, 480, ' ► ',{ fontSize:'64px', fill:'#8357eb',fontFamily: 'verdana, arial, sans-serif'}).setInteractive();
jumpButton=this.add.text(650, 480, ' ⯄ ',{ fontSize:'64px', fill:'#8357eb',fontFamily: 'verdana, arial, sans-serif'}).setInteractive();
FullScreen=this.add.text(700, 1, ' ⎚ ',{ fontSize:'64px', fill:'#8357eb',fontFamily: 'verdana, arial, sans-serif'}).setInteractive();
LeftButton.on('pointerdown', () => player.setVelocityX(-250));
RightButton.on('pointerdown', () => player.setVelocityX(250));
jumpButton.on('pointerdown', () => player.setVelocityY(-500));
FullScreen.on('pointerdown', () => this.scale.startFullscreen());
}
update(){}
}

Hey there,

I’m fairly new to Phaser 3 so for what it’s worth this is how I do it.

I have an additional game over scene.

So when my character falls into a pit I run:

var manCamera = this.cameras.main
manCamera.shake(250)
mainCamera.on('camerafadeoutcomplete', function () {
    this.scene.start('GameOver', { level: <'your-current-level-scene-name'> });
})

Hope it helps
Moe

1 Like

I noticed yr setVelocity is in create instead of update.

When moving or updating stuff, place them the the update() function instead.

Rever to first phaser Tutorial for sample codes.

Check for conditions, if met, just scene.start a game over scene.