We need to specify how long the animation should take, and measure how long it has taken so far.
- Create two new number variables,
animationTime
andelapsedTime
. SetanimationTime
to something long for now, like3
, and letelapsedTime
start at0
. - We can figure out how much time has passed using a handy property of the game called
frameDeltaTime
. It measures, in seconds, how long each tick takes. - Just after the line where you increment the chest’s Y position, increment
this.elapsedTime
bythis.game.frameDeltaTime
.
this.elapsedTime += this.game.frameDeltaTime
- Add another line to log the value of
elapsedTime
to the console, so that we can see how long the animation currently takes.
console.log(this.elapsedTime)