Create button listening

Source: Internet
Author: User

// 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;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.