Hi everyone !
I’m creating à game with es6 modern Phaser boilerplate, and here is my problem :
I’ve created a class, extends the Phaser.Physics.Arcade.Sprite class :
export default class Heroe extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y, id_heroe) {
switch (id_heroe) {
case 1:
super(scene, x, y, 'player-idle')
break;
default:
break;
}
scene.add.existing(this)
scene.physics.add.existing(this)
//HEROES ANIMATIONS
//idle
scene.anims.create({
key: 'idle',
frames: scene.anims.generateFrameNumbers('player-idle'),
frameRate: 20,
repeat: -1
})
//run
scene.anims.create({
key: 'run',
frames: scene.anims.generateFrameNumbers('player-run'),
frameRate: 20,
repeat: -1
})
this.anims.play('idle')
this.setCollideWorldBounds(true)
//CREATE KEY CURSORS
scene.cursors = scene.input.keyboard.createCursorKeys()
scene.keyQ = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q)
scene.keyD = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D)
scene.keyZ = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z)
scene.keyE = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E)
scene.events.on(Phaser.Scenes.Events.UPDATE, this.handleMovement, this)
}
handleMovement(){
let speedX = 200;
if (this.scene.cursors.left.isDown || this.scene.keyQ.isDown) {
this.setVelocityX(-speedX)
this.setFlipX(true)
this.play('run', true)
}
else if (this.scene.cursors.right.isDown || this.scene.keyD.isDown) {
this.setVelocityX(speedX);
this.setFlipX(false);
this.play('run', true)
}
}
}
My problem is : when I press a key, the ‘heroe’ go on the right direction but never stop, it’s my first time with extends class and all the news of ES6 and i don’t understand this comportment…
In my previous games, setVelocity apply a force to the sprite and it stoped after a moment, but not here?
Please need help, thank you !!!
EDIT : I forget the else{ setVelocity(0,0);}
… Sorry :,)