Jump to content

Time elapsed since last frame


Recommended Posts

Phaser provide few variants how to calc time elapsed since last frame, like game.time.elapsed or this.game.time.elapsedSince ect. But all that variants works badly %). 
For example, I want create moving sprite. It should be smooth and moving speed should not depend on device performance. 
Here simple code example:
private mySprite : Phaser.Sprite;private update(): void{    var dt : number = this.game.time.elapsed;    this.mySprite.x += dt * speed_const;}

For some reason mySprite moving speed depends on FPS. If FPS drops down mySprite moves faster. So game.time.elapsed contain wrong value %). 


Another way:

private mySprite : Phaser.Sprite;private lastTime : number = 0;private update() : void{        var tm : number = this.game.time.totalElapsedSeconds();    var dt: number = tm - this.lastTime;    this.lastTime = tm;    this.mySprite.x += dt * speed_const;}

mySprite moving speed does not depend on FPS, but mySprite moves jerkily because sometime dt == 0.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...