Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 玩家命中动画未在phaser中播放_Javascript_Animation_Game Engine_Game Physics_Phaser Framework - Fatal编程技术网

Javascript 玩家命中动画未在phaser中播放

Javascript 玩家命中动画未在phaser中播放,javascript,animation,game-engine,game-physics,phaser-framework,Javascript,Animation,Game Engine,Game Physics,Phaser Framework,我现在在Phaser中有一个非常简单的游戏,有一个玩家有向左行走和向右行走的动画,一个有平台、敌人等的关卡。我的一切都很好,除了球员的命中率和球员的死角。我认为这将是一样简单,添加动画,然后在事件中播放它们,但它不会工作,无论我做什么。下面是代码的快速分解: create() { this.player = this.game.add.sprite(50, this.game.world.height - 850, 'dude'); this.game.physics.arcad

我现在在Phaser中有一个非常简单的游戏,有一个玩家有向左行走和向右行走的动画,一个有平台、敌人等的关卡。我的一切都很好,除了球员的命中率和球员的死角。我认为这将是一样简单,添加动画,然后在事件中播放它们,但它不会工作,无论我做什么。下面是代码的快速分解:

create() {
    this.player = this.game.add.sprite(50, this.game.world.height - 850, 'dude');
    this.game.physics.arcade.enable(this.player);
    this.player.body.bounce.y = 0.2;
    this.player.body.gravity.y = 300;
    this.player.body.collideWorldBounds = true;
    this.player.animations.add('right', [0, 1, 2, 3, 4, 3, 2, 1], 10, true);
    this.player.animations.add('left', [7, 8, 9, 10, 11, 10, 9, 8], 10, true);
    this.player.animations.add('player_hit', [13, 14, 15, 14], 10, true);
    //lots of other code to build the world and enemies and such
}
update() {}
    this.game.physics.arcade.collide(this.player, this.enemies, this.playerHit, null, this);
    //lots of other code for walking, jumping, and all other things;
}
playerHit(player, enemy) {
    player.animations.stop();
    player.animations.play('player_hit');
    let life = this.lives.getFirstAlive();
    if(life) {
        life.kill();
    }
    if(this.lives.countLiving() < 1) {
         this.killPlayerAndEndGame();
    }
    enemy.kill();
}
一旦玩家离开了生命,tween将制作动画,他将漂浮起来,然后离开这个世界,但是那个愚蠢的玩家不会播放动画。在整个二人组中,玩家只是站在第0帧上

我知道一个事实,player\u hit动画可以工作,因为我非常恼火,用更新方法中的player\u hit动画替换了正确的移动,它播放了player\u hit动画。像这样:

update() {
    //code for collisions and whatnot;
    if(this.cursors.right.isDown) {
        this.direction = this.directions.LEFT;
        this.player.body.velocity.x = 200;
        this.player.animations.play('player_hit');
    }
    //other code for other things
}

现在,当我按下右箭头键时,播放动画。因此,我知道动画很好,但由于某些原因,当播放器被击中或播放器被杀死时,动画将不会播放。

可能是在“更新”循环中,您正在通过碰撞功能更改动画后更改动画?如果这是问题所在,您可以向碰撞函数添加一个标志,因此当循环转到动画部分时,它会首先检查是否有“player_hit”(通过检查标志状态),然后不要更改动画状态。@jolmos完美主意。我在我的更新方法中添加了一个检查,以查看当前动画是否为player_hit,以及动画循环是否小于1,如果是,则不执行任何操作,如果不是,则运行常规更新方法,并且它工作得非常好。我希望有一种更简单的方法告诉应用程序,如果某个动画正在运行,不要停止它。。不管怎样,这个想法成功了,非常感谢。可能是在“更新”循环中,在碰撞功能更改动画后,您正在更改动画?如果这是问题所在,您可以向碰撞函数添加一个标志,因此当循环转到动画部分时,它会首先检查是否有“player_hit”(通过检查标志状态),然后不要更改动画状态。@jolmos完美主意。我在我的更新方法中添加了一个检查,以查看当前动画是否为player_hit,以及动画循环是否小于1,如果是,则不执行任何操作,如果不是,则运行常规更新方法,并且它工作得非常好。我希望有一种更简单的方法告诉应用程序,如果某个动画正在运行,不要停止它。。无论如何,这个想法成功了,非常感谢。
update() {
    //code for collisions and whatnot;
    if(this.cursors.right.isDown) {
        this.direction = this.directions.LEFT;
        this.player.body.velocity.x = 200;
        this.player.animations.play('player_hit');
    }
    //other code for other things
}