Is this a good implementation of the gameloop

試著忘記壹切 提交于 2019-12-01 10:52:04

Youre on the right track - assuming that onEnterFrame is triggered in some way by Event.ENTER_FRAME - instead of skipping update, call it on every frame but pass in the time elapsed:

private function onEnterFrame() {
    var now = new Date().getTime();
    var delta = now - _lastTime;
    _lastTime = now;
    updateGameState(delta/1000);//divide by 1000 to give time in seconds
}

In updateGameState, you can utilise 'delta' to calculate movement etc, eg:

function updateGameState(timeElapsed:Number):void {
    myAlien.x += myAlienSpeedPerSecond*timeElapsed;
}

This way you get smooth movement even when frame rate varies.

from the Starling introduction pages, it shows that time elapsed is built into the EnterFrameEvent class.

// the corresponding event listener
private function onEnterFrame(event:EnterFrameEvent):void
{
    trace("Time passed since last frame: " + event.passedTime);
    enemy.moveBy(event.passedTime * enemy.velocity);
}

http://wiki.starling-framework.org/manual/animation#enterframeevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!