Jump to content

Car movement , collision and audio


Garvit
 Share

Recommended Posts

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.

Quote

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({

Quote
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;
        }
    }

}

});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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