Problem: Sprite acceleration/deceleration using timer

I’m writing a simple space game (it looks like Asteroids right now) that, at this early point, only shows a little spaceship, and has one bit of functionality: You press the spacebar to make it go, and let up on the spacebar to make it stop. It only goes straight.

I have used timers and keyboard events to make this work. It’s easy to get the ship to go and stop, but now I want to add that first bit of realism by having the ship decelerate when you lift the spacebar, rather than just stopping on a dime.

What I’ve developed so far has shown me that this deceleration either doesn’t work in a timer function (I’m a newbie and may not be using the correct lingo here), or I somehow have to do something to sync frame rate with timer delay or something along those lines. Right now, with the code I have (shown below), the trace indicates that the ship is indeed “slowing down” until it stops, but because of the timer, that happens pretty much instantly.

Can anyone advise me about how to make the ship slow down in a realistic fashion? (That would of course also show me how to make it speed up realistically too.)

Comments about my coding style are welcome. I am not a developer by trade.

Here’s the code. This is ALL in frame one of a FLA. No custom classes. Frame rate is 50.


var ship:Sprite = new Sprite();
ship.graphics.lineStyle(2,0xFFFFFF);
ship.graphics.beginFill(0x000066);
ship.graphics.lineTo(40, 20);
ship.graphics.lineTo(0, 40);
ship.graphics.lineTo(10, 20);
ship.graphics.lineTo(0, 0);
ship.graphics.endFill();
var flame:Sprite = new Sprite();
flame.graphics.lineStyle(2, 0xFFFFFF);
flame.graphics.beginFill(0x000066);
flame.graphics.moveTo(6, 12);
flame.graphics.lineTo(10, 20);
flame.graphics.lineTo(6, 28);
flame.graphics.lineTo(-2, 20);
flame.graphics.lineTo(6, 12);
flame.graphics.endFill();
addChild(ship);
ship.addChild(flame);
var speed:Number = 1;
var i:Number;
ship.x = stage.stageWidth/2 - ship.width/2;
ship.y = stage.stageHeight/2 - ship.height/2;
var shipGoTimer:Timer = new Timer(10);
shipGoTimer.addEventListener(TimerEvent.TIMER, shipGo);
var shipStopTimer:Timer = new Timer(10);
shipStopTimer.addEventListener(TimerEvent.TIMER, shipStop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, sbDown);// press spacebar to move
stage.addEventListener(KeyboardEvent.KEY_UP, sbUp);
function sbDown(event:KeyboardEvent):void {
 if (event.charCode == 32) {//spacebar
  shipGoTimer.start();
  shipStopTimer.stop();
 }
}
function sbUp(event:KeyboardEvent):void {
 if (event.charCode == 32) {
  i = 0;
  shipGoTimer.stop();
  shipStopTimer.start();
 }
}
function shipGo(event:TimerEvent):void {
 ship.x += speed;
 // if ship goes beyond right bound
 if ((ship.x - ship.width/2) > stage.stageWidth) {
  ship.x = -ship.width * 0.9;
 }
}
function shipStop(event:TimerEvent):void {
 while (i < speed) {
  ship.x += speed - i;
  trace("i = " + i + ", ship.x = " + ship.x);
  if ((ship.x - ship.width/2) > stage.stageWidth) {
   ship.x = -ship.width * 0.9;
  }
  i += .01;
 }
}