Is there a code to speed up animation when a key is pressed?

I want to sort of speed up the animation speed of sprites of an entity when a key is pressed, but I can’t seem to figure it out, any sort of help will be greatly appreciated :slight_smile:

Hi @sayhimeee,

So you can do that like this inside the update method of a script attached to your sprite entity:

if( this.app.keyboard.isPressed( pc.KEY_SPACE ){
   this.entity.sprite.speed = 3;
}else{
   this.entity.sprite.speed = 1;
}
2 Likes

Hi, thank you for helping out! But for some reason, the speed isn’t reverting back once it has changed even though it’s under the update method?

That’s strange, releasing the space key should reset the speed. Can you check what the sprite speed at the end of your update method?

Either with the debugger or using a console log:

console.log(this.entity.sprite.speed);
1 Like

I checked with the debugger, it seems that I set both speeds to be too fast, I didn’t know the default should be 1, I thought it was the same as the numbers set in fps per sprite animation. Thank you again, Leonidas!

1 Like