Hey everyone,
I’m currently working on some character movement. I want to switch to a different index of the spritesheet, one for an idle animation, one for a running animation, depending if I press a directional key (in this case, D as in WASD keys) .
I figured id make two anim configs, one for the idle animation and the other for the running animation. Id load them, and initiate the play and resume function depending on if something is pressed. I initiated the play method inside the player update() function.
I haven’t tried to make a player create() function, and realized that maybe it would work with the update function. Im having trouble trying to find a demonstration and example on triggering animations, depending on with a key is pressed.
I had attempted to try what I had below, but the animation would stop while im pressing the key down, and would only run after I release. Also the run animation seemed sped up. Is this because the key pressed needs to be in its own player create() function, and shouldnt be in the player update() function? Not sure.
Heres my code:
import Phaser from 'phaser'
export class Player extends Phaser.Physics.Arcade.Image {
constructor(scene, x, y, key, frame) {
super(scene, x, y, key, frame)
this.scene = scene;
this.velocity = 160;
// enable physics
this.scene.physics.world.enable(this)
//set immovable if another object collides with our player
this.setImmovable(false)
// scale our player
this.setScale(2);
// collide with world bounds
this.setCollideWorldBounds(true);
// add the player to our existing scene
this.scene.add.existing(this)
// add the sprite
this.sprite = this.scene.add.sprite(0, 0, "adventurer").setScale(2)
// create animation config
var animIdle = {
key: 'idle',
frames: this.scene.anims.generateFrameNames('adventurer', { start: 0, end: 5}),
frameRate: 9,
yoyo: false,
repeat: -1
}
let animRun = {
key: 'run',
frames: this.scene.anims.generateFrameNames('adventurer', { start: 8, end: 13}),
framerate: 2,
yoyo: true,
repeat: -1
}
// add config to animation variable
this.scene.anims.create(animRun)
this.scene.anims.create(animIdle)
// load run animation?
this.sprite.anims.load('idle');
// play idle animation till specified otherwise
this.sprite.anims.play('idle');
// load run animation?
this.sprite.anims.load('run');
}
update(cursors) {
this.body.setVelocity(0);
// sprite will follow player vector
this.sprite.x = this.x
this.sprite.y = this.y
if(cursors.A.isDown) {
this.body.setVelocityX(-this.velocity);
// trigger run animation
this.sprite.anims.play('run');
} else if (cursors.D.isDown) {
this.body.setVelocityX(this.velocity);
}
if(cursors.W.isDown) {
this.body.setVelocityY(-this.velocity);
} else if (cursors.S.isDown) {
this.body.setVelocityY(this.velocity);
}
}
}
I would do more research but its late and will have to wait till tommorow.
If anyone could lead me in the right direction, I’d really appreciate it.
Thanks.