Hey everyone, I’m looking into creating a AS3 game and I am back to the place where all my nightmares come true…
The Game Loop
This portion, this critical component has crippled me. Because I’m always attempting to implement a fixed timestep game loop, but for some reason I have a very noticeable jitter/jerk moment. Nothing is going on in my game other than a 32x32 box moving around with key presses, which is blited to a main canvas.
I have read this this article http://www.koonsolo.com/news/dewitters-gameloop/ and who could forget http://gafferongames.com/game-physics/fix-your-timestep/
Unfortunately, I can’t seem to get it ‘working’ using either approach. It seems as if something has gone horribly wrong with my interpolation or set up of the loop.
Could someone please give me some assistance on this?
Here is all the needed code, it uses the Final Touch Gafferon games approach
maybe someone can point out what is wrong
Thanks
Noodle
// Above other init code ( game object, image loads, now and last Number vars, etc); Stage's frame rate is set to 60
//Main Game loop; executed on the ENTER_FRAME event
public var fixedDT:Number = 1/60; //Shoot for 60 FPS = 0.016666667
public var accumulator:Number = 0;
private function mainLoop(e:Event):void
{
//Call the delta time
now = getTimer();
deltaTime = (now - last) / 1000;
last = now;
//Add it to the accumulator
accumulator += deltaTime;
//While we have enough time update
while (accumulator >= fixedDT)
{
update(fixedDT);
accumulator -= fixedDT;
}
//render by passing in the interpolation ratio
render(accumulator/fixedDT);
}
//Render method, blit stuff to the screen; Takes in the interpolation ratio
public function render(inter:Number):void
{
//Clear the screen and lock the canvas down for blits
canvas.fillRect(canvas.rect, 0x000000FF);
canvas.lock();
//Draw our game object; pass in the interpolation value
boxObject.draw(inter);
canvas.unlock();
}
//Box Object up date and draw
public function update(dt:Number):void
{
prevPosition = position;
if(Main.keyboard.checkKey(Main.keyboard.KB_RIGHT))
position.x += 120 * dt;
if(Main.keyboard.checkKey(Main.keyboard.KB_LEFT))
position.x -= 120 * dt;
}
//Draw method; Interpolate based on the interpolation value
public function draw(inter:Number):void
{
renderPosition.x = n * position.x + ((1.0 - inter) * prevPosition.x);
renderPosition.y = n * position.y + ((1.0 - inter) * prevPosition.y);
Main.canvas.copyPixels(image, image.rect, renderPosition);
}