It would figure that success breeds more success, and now I have a ship that flys about the screen using the arrow keys, again controlled via a Ship class. Try it for yourself right here!

[Download the source files](http://flashforlearning.com/files/Asteroids_02.zip).

Again, much like the [original source](http://www.macromedia.com/devnet/mx/flash/articles/asteroids05.html), the class structure as presented below looks similar. Since the asteroids move about with their own behaviors, and those behaviors are really separate from the ship’s behaviors, it’s easy to do this really clean.

I’m very curious about how these items will interact, and more pertinent to me, what that resulting code will be like.

In the meantime, if you’re playing along at your own computer, create a Ship movieclip and bind it to a Ship.as class file, which looks like this:

```
class Ship extends MovieClip
{
public var torque:Number;
public var rtorque:Number;
public var AngVelocity:Number;
public var AngAcceleration:Number;
// angle is the ship's rotation angle.
public var angle:Number;
public var force:Number;
// xv is the ship's x-velocity (going across)
public var xv:Number;
// yv is the ship's y-velocity (going up/down)
public var yv:Number;
// x is the ship's x-position.
public var x:Number;
// y is the ship's y-position.
public var y:Number;
// xa is the ship's x-acceleration (how it speeds up/slows down)
public var xa:Number;
// ya is the ship's y-acceleration (how it speeds up/slows down)
public var ya:Number;
// v is the ship's "vector"
public var v:Number;
// the ship has a "momentum" factor to slow down some response time.
public var momentum:Number;
// drag will use the momentum factor
public var drag:Number;
// We're going to put a timer in for the firing engine, so we don't fire all our
// lasers at the same time - spacing out the firing to make better shooting.
public var timer:Number;
// We'll set up an array of object references to each laser.
//public var lasers:Array;
private function Ship()
{
angle = 0;
xv = 0;
x = 0;
y = 0;
yv = 0;
xa = 0;
ya = 0;
v = 0;
momentum = 0.02;
drag = 0;
timer = 0;
AngVelocity = 0;
AngAcceleration = 0;
}
public function onEnterFrame()
{
// RIGHT and LEFT control the amount of torque applied to the ship's movement
if ( Key.isDown( Key.RIGHT ) )
{
torque = 5;
}
else if ( Key.isDown( Key.LEFT ) )
{
torque = -5;
}
else
{
torque = 0;
}
rtorque = 0.5 * AngVelocity;
AngAcceleration = torque - rtorque;
AngVelocity += AngAcceleration;
angle += AngVelocity;
// constrain theta 0 < 360
// We only want to deal with angles from 0 to 360 degrees
if ( angle < 0 )
{
angle += 360;
}
if ( angle > 360 )
{
angle -= 360;
}
v = Math.sqrt( Math.pow( xv, 2) + Math.pow( yv, 2) );
drag = momentum * v;
// Here we control the thrust and acceleration of the ship
force = ( Key.isDown( Key.UP ) ) ? 1 : 0;
xa = ( ( drag * xv ) * -1 ) + ( force * Math.sin( Math.PI / 180 * angle ) );
ya = ( ( drag * yv ) * -1 ) - ( force * Math.cos( Math.PI / 180 * angle ) );
xv += xa;
yv += ya;
// Just like the Asteroids, we need to constrain the ship within
// the bounds of the playable area.
if ( _x < = 10 )
{
_x += 390;
}
if ( _x >= 390 )
{
_x -= 390;
}
if ( _y < = 10 )
{
_y += 140;
}
if ( _y >= 140 )
{
_y -= 140;
}
// And here we finally assign and update the ship's x- and
// y- positions, and its rotation angle.
_y += yv;
_x += xv;
_rotation = angle;
// ... END movement
}
}
```

Commments? Suggestions? Bugs?