Phaser Physics -- Movement -- Collision
var game = new Phaser.Game( | |
200, //The width in pixels | |
200, //The height in pixels | |
Phaser.AUTO, //The render system | |
"phaser", //The element | |
{ | |
preload: preload, // The preload function | |
create: create, // The create function | |
update: update // The update function | |
} | |
); | |
// Create the variables outside of any one function scope | |
var sprite; | |
var sprite2; | |
function preload() {} | |
function create() { | |
// Start the Arcade physics system | |
game.physics.startSystem(Phaser.Physics.ARCADE); | |
sprite = game.add.sprite( | |
50, // The x position | |
50, // The y position | |
'sprite' // The image cache-key | |
); | |
sprite2 = game.add.sprite( | |
50, // The x position | |
100, // The y position | |
'sprite' // The image cache-key | |
); | |
// Enable Arcade physics on 'sprite' | |
game.physics.arcade.enable(sprite); | |
// Enable Arcade physics on 'sprite2' | |
game.physics.arcade.enable(sprite2); | |
// Make sure 'sprite' does not leave the world | |
sprite.body.collideWorldBounds = true; | |
// Make sure 'sprite2' does not leave the world | |
sprite2.body.collideWorldBounds = true; | |
} | |
function update() { | |
sprite.body.velocity.y = 0; | |
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { | |
sprite.body.velocity.y = -40; // Move up | |
} | |
if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { | |
sprite.body.velocity.y = 40; // Move down | |
} | |
// Check if 'sprite' and 'sprite2' collide with each other | |
game.physics.arcade.collide(sprite, sprite2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment