简体   繁体   English

一种在Phaser框架中切换精灵动画的方法

[英]A way to toggle between a sprite's animations in the Phaser framework

Is there a way to toggle between a sprite's animations? 有没有办法在精灵的动画之间切换? For example, I have a sprite touching the ground, but when it leaves the ground, I want it to play a different animation than the one that it plays while on the ground. 例如,我有一个精灵触摸地面,但当它离开地面时,我希望它播放的动画不同于它在地面上播放的动画。 However, I also want it to play the the first animation when it touches the ground again. 但是,我也想让它在第一个动画再次触地时播放。 I basically want something like this: 我基本上想要这样的东西:

if (sprite.body.touching.down === false) {
    sprite.animations.toggleAnimation("different_animation");
} else {
    sprite.animations.play("original_animation");
}

Does Phaser have an easy way of doing this? Phaser有一个简单的方法吗?

You can add as many animations to a Sprite as you need and then play them using the key. 您可以根据需要为Sprite添加任意数量的动画,然后使用该键播放它们。 For example: 例如:

sprite.animations.add('jump', [0,1,2,3]);
sprite.animations.add('crouch', [4,5]);
sprite.animations.add('walk', [6,7,9,10,11]);

Then you can just play the animations by their key: sprite.play('walk') 然后你可以通过他们的键来播放动画: sprite.play('walk')

It looks like I've found a solution to this; 看起来我找到了解决方案; what I've been needing to do is load a different texture onto the sprite, not necessarily play a different animation. 我一直需要做的是在精灵上加载不同的纹理 ,不一定要播放不同的动画。 I guess I didn't communicate that very well. 我想我没有那么好地沟通。 Basically, what I'm saying is this example: 基本上,我所说的是这个例子:

http://phaser.io/examples/v2/animation/change-texture-on-click http://phaser.io/examples/v2/animation/change-texture-on-click

to make things easy my approch is: 为了方便我的approch是:

 animation_arr = ['idle', 'walk', 'jump', 'idle_towel', 'walk_towel', 'jump_towel' ];
 for(var i=0; i < animation_arr.length; i++){
    player.animations.add(animation_arr[i], [0+(i*10), 1+(i*10), 2+(i*10), 3+(i*10), 4+(i*10), 5+(i*10), 6+(i*10), 7+(i*10), 8+(i*10), 9+(i*10)], 10, true);
 }

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

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