我正在嘗試使用Sprite工作表來移動箭頭鍵的字符,但似乎沒有工作。如果我將背景設置爲大於500,500,屏幕會與角色一起移動,但我希望角色能夠自由移動,而不會隨背景移動。無法在Phaser.js中動畫Sprite
我可以在我的代碼中更改哪些字符,以便使用箭頭鍵移動字符?並使動畫實際工作?
window.onload = function() {
var game = new Phaser.Game(500, 500, Phaser.AUTO, 'phaser-example',{ preload: preload, create: create });
function preload() {
game.stage.backgroundColor = '#fc6b84';
game.load.spritesheet('player', 'reddude.png', 64, 64);
}
function create() {
// This simply creates a sprite using the player image we loaded above and positions it at 200 x 200
var test = game.add.sprite(250, 250, 'player');
player.animations.add('walk');
player.anchor.setTo(0.5, 1);
game.physics.arcade.enable(player);
player.body.collideWorldBounds = true;
addItems();
addPlatforms();
cursors = game.input.keyboard.createCurosrKeys();
//game set up
}
function update() {
game.physics.arcade.collide(player, platforms);
game.physics.arcade.overlap(player, items, itemHandler);
game.physics.arcade.overlap(player, badges, badgeHandler);
player.body.velocity.x = 0;
// is the left cursor key presssed?
if (cursors.left.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.x = -50
player.scale.x = - 1
}
// is the right cursor key pressed?
else if (cursors.right.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.x = 50
player.scale.x = 1
}
else if (cursors.up.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.y = 50
player.scale.y = 1
}
else if (cursors.down.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.y = -50
player.scale.y = -1
}
// player doesn't move
else {
player.animations.stop();
}
}
}
您的代碼根本不起作用。從這個開始(https://jsfiddle.net/archierocks183/zygz2ksm/17/),並讓它進入工作階段,以獲得更好的幫助 –