Skip to content

Instantly share code, notes, and snippets.

@photonstorm
Created August 5, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save photonstorm/dd2aa4eaff2447257f1801a8d7921a2e to your computer and use it in GitHub Desktop.
Save photonstorm/dd2aa4eaff2447257f1801a8d7921a2e to your computer and use it in GitHub Desktop.
Add body to shape
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
backgroundColor: '#0072bc',
physics: {
default: 'arcade',
arcade: {
debug: true
}
},
scene: {
create: create,
update: update
}
};
var cursors;
var player;
var game = new Phaser.Game(config);
function create ()
{
player = this.add.rectangle(400, 300, 64, 64, 0xffffff);
this.physics.add.existing(player, false);
cursors = this.input.keyboard.createCursorKeys();
player.body.setCollideWorldBounds(true);
}
function update ()
{
player.body.setVelocity(0);
if (cursors.left.isDown)
{
player.body.setVelocityX(-300);
}
else if (cursors.right.isDown)
{
player.body.setVelocityX(300);
}
if (cursors.up.isDown)
{
player.body.setVelocityY(-300);
}
else if (cursors.down.isDown)
{
player.body.setVelocityY(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment