var cursor_keys, ship_sprite; let game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, 'Move a sprite'); game.state.add('main', { preload: function(e) { game.load.image('ship_image', '{{ file:space-ship.png }}'); }, create: function(e) { block_group = null; obstacle_group = null; game.stage.backgroundColor = '#ff0000'; cursor_keys = this.input.keyboard.createCursorKeys(); cursor_keys.down.onDown.add(this.input_, this); cursor_keys.left.onDown.add(this.input_, this); cursor_keys.right.onDown.add(this.input_, this); cursor_keys.up.onDown.add(this.input_, this); ship_sprite = game.add.sprite(180, 180, 'ship_image'); game.physics.arcade.enable(ship_sprite); ship_sprite.anchor.set(0.5); }, input_: function(e) { if (cursor_keys.up.isDown && e !== cursor_keys.up) { ship_sprite.y -= 2; ship_sprite.angle = 0; } else if (cursor_keys.down.isDown && e !== cursor_keys.down) { ship_sprite.y += 2; ship_sprite.angle = (-180); } else if (cursor_keys.left.isDown && e !== cursor_keys.left) { ship_sprite.x -= 2; ship_sprite.angle = (-90); } else if (cursor_keys.right.isDown && e !== cursor_keys.right) { ship_sprite.x += 2; ship_sprite.angle = 90; } }, update: function(e) { if (e) { this.input_(e); } game.world.wrap(ship_sprite, 0); }, render: function(e) { game.debug.spriteInfo(ship_sprite, 32, 32); }, }, true);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter