简体   繁体   English

无法在Phaser.js中为Sprite设置动画

[英]Unable to Animate Sprite in Phaser.js

I am trying to use a sprite sheet to move a character around with the arrow keys, but it doesn't seem to be working. 我试图使用精灵表用箭头键移动一个字符,但它似乎没有工作。 If I set the background to be larger than 500, 500, the screen will move around along with the character, but I want the character to move around freely without the background moving with it. 如果我将背景设置为大于500,500,则屏幕将随角色一起移动,但我希望角色自由移动而不会使背景随之移动。

What can I change in my code to make the character move by using the arrow keys? 我可以在代码中更改什么来使用箭头键移动角色? And make the animations actually work? 并使动画真正起作用?

这是我的图片,它是256x256

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();
    }
  }

}

You can make a camera follows your player. 您可以让相机跟随您的播放器。 First crate player sprite then add this line: 第一个crate播放器精灵然后添加这一行:

game.camera.follow(player);

You can find on this link what you need. 您可以在此链接中找到所需内容。 https://phaser.io/examples/v2/camera/basic-follow https://phaser.io/examples/v2/camera/basic-follow

Also, shouldn't you declare your variable as "var player" instead of "var test" inside create function? 另外,你不应该在create function中将变量声明为“var player”而不是“var test”吗?

You add a sprite variable called test , but you add the animation to a variable called player . 添加一个名为test的sprite变量,但是将动画添加到名为player的变量中。 This could be a mistake you've made, I mean where is var player defined? 这可能是你犯的错误,我的意思是var player定义在哪里?

As for the different animations to work, you have to add each animation separately to your sprite variable. 至于要使用的不同动画,您必须将每个动画分别添加到您的精灵变量中。 You have to indicate which frames are for the "walking left" animation, which frames for the "walking up" animation etc. and create separate animations. 您必须指出哪些帧用于“向左走”动画,哪些帧用于“向上走动”动画等,并创建单独的动画。 Something like this: 像这样的东西:

// define variable globally, outside "create()", so then "update" can also use the variable
var playersprite;

// create a sprite in the "create()" function
// note: playersprite is the variable name and 'player' is the spritesheet name string
playersprite = game.add.sprite(250, 250, 'player');

// add animations to sprite
playersprite.animations.add('walk_down',  [0,1,2,3]);
playersprite.animations.add('walk_left',  [4,5,6,7]);
playersprite.animations.add('walk_right', [8,9,10,11]);
playersprite.animations.add('walk_up',    [12,13,14,15]);

And then depending on the direction player moving, play a different animation. 然后根据玩家移动的方向,播放不同的动画。

// when LEFT cursor key presssed
if (cursors.left.isDown) {
    playersprite.animations.play('walk_left', 10, true);
    // etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM