In this example, I made a driving simulation. In this you can move around as you wish by pressing left and right key. It also has a boundary in which if you collide your car will crash and a car crash audio will come and the game will restart.
var config = {
type: Phaser.CANVAS,
width: 1500,
height: 750,
physics: {
default: ‘arcade’
},
backgroundColor: 0x27ae60,
scale: {
autoCenter: Phaser.Scale.CENTER_BOTH
},
scene:[GameScene1]
};
var game = new Phaser.Game(config);
var GameScene1 = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function GameScene1 ()
{
Phaser.Scene.call(this, { key: 'GameScene1' });
},
preload: function (){
this.load.image('car1' , "assets/car1.jpg");
this.load.audio('crash' , "assets/crash.mp3")
},
create: function ()
{
car1 = this.add.image(200, 400, 'car1');
music = this.add.audio('crash');
cursors = this.input.keyboard.createCursorKeys();
car1.setCollideWorldBounds(true);
},
update: function(){
if (cursors.right.isDown){
if (car1.x != 1500) {
car1.x += 5;
}
else {
music.play()
this.scene.scene.start('GameScene1');
}
}
if (cursors.left.isDown) {
if(car1.x !=200 ){
car1.x -= 5;
}
}
}
});