I have a very simple game right now in Phaser, there is a player with walk left and walk right animations, a level with platforms, enemies, and such. I have everything working great except for the player hit and player dead pieces. I thought it would be as simple as adding in the animations then playing them on events, but it will not work, no matter what I do. A very quick breakdown of the code is here:
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();
}
Now, all of that works except the animation. The player will lose a life, I can see the life get removed from the display and everything, but the player just stands there. The player_hit animation is never played. He collides with the enemy, the life is removed, the enemy is killed, but the player just stands there in frame 0. I can't figure out why the player will not animate. I even added the animation into the player kill method like so:
killPlayerAndEndGame() {
this.player.animations.stop();
this.player.animations.play('player_hit', 10, true);
let startPOS = this.player.position.y;
let upPOS = startPOS - 200;
this.playerDead = this.game.add.tween(this.player).to({y: upPOS}, 1000, false).to({y: this.game.world.height - 100}, 1500, false);
this.playerDead.start();
//more code to show score and whatnot
}
Once the player is out of lives the tween will animate and he will float up, then out of the world, but that stupid player_hit animation will not play. throughout the entire tween the player is just standing there on frame 0.
I know for a fact that the player_hit animation works because I got really annoyed and replaced the right movement with the player_hit animation in the update method and it played the player_hit animation. Like so:
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
}
Now when I hit the right arrow key the player_hit animation plays. So I know the animation is good, but for some reason it will not play when the player is hit, or the player is killed.