// Create an empty clip ship_mc to draw a ship
This. createEmptyMovieClip ("ship_mc", this. getNextHighestDepth ());
Function drawShip (flag: Boolean): Void {
With (ship_mc ){
Clear ();
LineStyle (1, 0 xffffff );
MoveTo (10, 0 );
LineTo (-10,-10 );
LineTo (-5, 0 );
LineTo (-10, 10 );
LineTo (10, 0 );
// Whether to display the exhaust gas during spacecraft acceleration
If (flag ){
MoveTo (-7.5,-5 );
LineTo (-15, 0 );
LineTo (-7.5, 5 );
}
}
}
// The initial status does not show tail gas
DrawShip (false );
// Initialize the Spacecraft location
Ship_mc. _ x = Stage. width/2;
Ship_mc. _ y = Stage. height/2;
// Initialize the rotation velocity, the force corresponding to the acceleration, and the initial speed in the x and y directions.
Var vr: Number = 0;
Var force: Number = 0;
Var vx: Number = 0;
Var vy: Number = 0;
// Add the listening keyboard and buttons in the left and right directions.
Obj = new Object ();
Obj. onKeyDown = function (obj: Object ){
If (Key. isDown (Key. LEFT )){
// Left arrow key, clockwise rotation
Vr + = 5;
} Else if (Key. isDown (Key. RIGHT )){
// Right arrow key, clockwise rotation
Vr-= 5;
} Else if (Key. isDown (Key. UP )){
// Apply a force to the upward direction to display the exhaust gas
Force = 0.2;
DrawShip (true );
}
};
Obj. onKeyUp = function (obj: Object ){
// Release the button
Vr = 0;
Force = 0;
DrawShip (false );
};
Key. addListener (obj );
// Execute the function
This. onEnterFrame = function (){
// Control the rotation of a ship. If you hold down the direction key for a long time, the ship will rotate faster and faster;
Ship_mc. _ rotation + = vr;
Var angle: Number = ship_mc. _ rotation * Math. PI/180;
// Separate the force-generated acceleration in the direction of the spacecraft into the x and y directions.
Var ax: Number = force * Math. cos (angle );
Var ay: Number = force * Math. sin (angle );
// Mobile spacecraft
Vx + = ax;
Vy + = ay;
Ship_mc. _ x + = vx;
Ship_mc. _ y + = vy;
};