Flash/flex Study Notes (23): kinematics principles

Source: Internet
Author: User

First, write a public ball:

 
Package {import flash. display. sprite; // small ball class public class ball extends sprite {private var radius: Number; // radius private var color: uint; // color public function ball (r: Number = 50, c: uint = 0xff0000) {This. radius = r; this. color = C; Init ();} private function Init (): void {graphics. beginfill (color); graphics. drawcircle (0, 0, radius); graphics. endfill ();}}}

Circular operation and elliptical motion:

The X and Y coordinates of objects are calculated by using trigonometric functions and elliptic formulas.

 var ball: ball = new ball (5, 0xff0000); var ball2: ball = new ball (8, 0x0000ff); addchild (ball); addchild (ball2); VaR _ radius: uint = 75; VaR _ angle: Number = 0; VaR _ centerx = stage. stagewidth/2; VaR _ centery = stage. stageheight/2; VaR _ radius_x = 100; VaR _ radius_y = 175; addeventlistener (event. enter_frame, enterframehandler); graphics. linestyle (1); graphics. moveTo (_ centerx, _ centery); function enterframehandler (Event: Event): void {ball. X = _ centerx + _ radius * Math. cos (_ angle * Math. PI/180); ball. y = _ centery + _ radius * Math. sin (_ angle * Math. PI/180); ball2.x = _ centerx + _ radius_x * Math. cos (_ angle * Math. PI/180); ball2.y = _ centery + _ radius_y * Math. sin (_ angle * Math. PI/180); _ angle + = 1; if (_ angle <= 360) {graphics. moveTo (ball. x, ball. y); graphics. linestyle (1, _ angle * Math. pow ()/36); // graphics. linestyle (1, math. random () * 0 xffffff, 1); graphics. lineto (ball2.x, ball2.y) ;}

 

Uniformly accelerated linear motion:
Speed formula: V = V0 + at. Although the physical formula is like this, you have to change the shorway in flash. The default value of Flash is 24 frames per second, the enterframe event is triggered when a new frame is entered, so we can roughly think that each frame is a "unit time". An important feature of uniform acceleration is that the speed per unit time increases by a fixed value, so in flash, you only need to add a fixed speed value in enterframe.

VaR ball: ball; var VX: Number = 0; var ax: Number = 0; var Vy: Number = 0; var ay: Number = 0; ball = new ball; addchild (ball); ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; ball. scalex = 0.1; ball. scale= 0.1; addeventlistener (event. enter_frame, enterframehandler); stage. addeventlistener (keyboardevent. key_down, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler); graphics. linestyle (1, 0, 1); graphics. moveTo (stage. stagewidth/2)-10, stage. stageheight/2); graphics. lineto (stage. stagewidth/2) + 10, stage. stageheight/2); graphics. moveTo (stage. stagewidth/2, (stage. stageheight/2)-10); graphics. lineto (stage. stagewidth/2, (stage. stageheight/2) + 10); function enterframehandler (Event: Event): void {VX + = ax; // each time you enter the frame, the specified value (that is, uniformly accelerated running on the X axis) is added to the speed of the X axis. X + = VX; Vy + = Ay; // each time you enter the frame, the specified value is increased for the speed of the Y axis (that is, the Y axis is uniformly accelerated) Ball. Y + = Vy; If (ball. x> = stage. stageWidth-ball.width/2) {ball. X = stage. stageWidth-ball.width/2; vx = 0;} else if (ball. x <= ball. width/2) {ball. X = ball. width/2; vx = 0;} If (ball. y> = stage. stageHeight-ball.height/2) {ball. y = stage. stageHeight-ball.height/2; Vy = 0 ;}else if (ball. Y <= ball. height/2) {ball. y = ball. height/2; Vy = 0 ;}} function keydownhandler (Event: keyboardevent): void {Switch (event. keycode) {Case keyboard. right: AX = 0.5; break; Case keyboard. left: AX =-0.5; break; Case keyboard. down: Ay = 0.5; break; Case keyboard. up: Ay =-0.5; default: break;} function keyuphandler (Event: keyboardevent): void {ax = 0; ay = 0 ;}

Free fall:
In fact, it is a special case of uniformly accelerated linear motion.CodeMake a slight modification.

Function enterframehandler (Event: Event): void {VX + = ax; ball. X + = VX; Vy + = Ay; Vy + = 0.45; // here, a value is forcibly added as the gravity acceleration, so when you do not control it, you will also drop the ball. Y + = Vy; If (ball. x> = stage. stageWidth-ball.width/2) {ball. X = stage. stageWidth-ball.width/2; vx = 0;} else if (ball. x <= ball. width/2) {ball. X = ball. width/2; vx = 0;} If (ball. y> = stage. stageHeight-ball.height/2) {ball. y = stage. stageHeight-ball.height/2; Vy * =-0.7; // assuming that the reverse speed after rebound is 70%} else if (ball. Y <= ball. height/2) {ball. y = ball. height/2; Vy = 0 ;}}

Rebound: You just need to modify the above example later.

VaR ball: ball; var VX: Number = 0; var ax: Number = 0; var Vy: Number = 0; var ay: Number = 0; ball = new ball; addchild (ball); ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; ball. scalex = 0.1; ball. scale= 0.1; addeventlistener (event. enter_frame, enterframehandler); stage. addeventlistener (keyboardevent. key_down, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler); graphics. linestyle (1, 0, 1); graphics. moveTo (stage. stagewidth/2)-10, stage. stageheight/2); graphics. lineto (stage. stagewidth/2) + 10, stage. stageheight/2); graphics. moveTo (stage. stagewidth/2, (stage. stageheight/2)-10); graphics. lineto (stage. stagewidth/2, (stage. stageheight/2) + 10); VaR _ bounce: Number =-0.5; // percentage of velocity after rebound function enterframehandler (Event: Event): void {VX + = ax; ball. X + = VX; Vy + = Ay; ball. Y + = Vy; If (ball. x> = stage. stageWidth-ball.width/2) {ball. X = stage. stageWidth-ball.width/2; VX * = _ bounce;} else if (ball. x <= ball. width/2) {ball. X = ball. width/2; VX * = _ bounce;} If (ball. y> = stage. stageHeight-ball.height/2) {ball. y = stage. stageHeight-ball.height/2; Vy * = _ bounce;} else if (ball. Y <= ball. height/2) {ball. y = ball. height/2; Vy * = _ bounce;} function keydownhandler (Event: keyboardevent): void {Switch (event. keycode) {Case keyboard. right: AX = 0.5; break; Case keyboard. left: AX =-0.5; break; Case keyboard. down: Ay = 0.5; break; Case keyboard. up: Ay =-0.5; default: break;} function keyuphandler (Event: keyboardevent): void {ax = 0; ay = 0 ;}

Round-trip linear motion:

 graphics. linestyle (1, 0xff0000, 0.5); graphics. moveTo (0, 0); graphics. lineto (stage. stagewidth, stage. stageheight); graphics. linestyle (1, 0x00ff00, 0.5); graphics. moveTo (stage. stagewidth, 0); graphics. lineto (0, stage. stageheight); var ball: ball = new ball (10, 0xff0000); ball. X = 0; ball. y = 0; VaR _ seedx =-90; VaR _ seedy = 0; var angle: Number = 45 * Math. PI/180; addchild (ball); var ball2: ball = new ball (10, 0x00ff00); ball2.x = stage. stagewidth; var speed = 2; var isdown = true; addchild (ball2); addeventlistener (event. enter_frame, enterframehandler); function enterframehandler (E: Event): void {ball. X = math. cos (_ seedx * Math. PI/180) * stage. stagewidth; ball. y = math. sin (_ seedy * Math. PI/180) * stage. stageheight; _ seedx + = 1; _ seedy + = 1; if (_ seedx> 90) {_ seedx =-90;} If (_ seedy> 180) {_ seedy = 0;} // downward if (isdown) {ball2.x-= speed; ball2.y + = speed; If (ball2.x <0) {isdown = false ;}} else {// up ball2.x-=-speed; ball2.y + =-speed; If (ball2.x> stage. stagewidth) {isdown = true ;}}

Note: The preceding two methods are used. The simplest way to move a round-trip to a uniform straight line is to increase the speed of X and Y-axis to a fixed value. If the speed is not required, use sin, cos functions may be simpler

Spacecraft keyboard control demonstration(From the example in ActionScript 3.0 animation)

Spacecraft

Package {import flash. display. sprite; public class ship extends sprite {public function ship () {draw (false);} public function draw (showflame: Boolean): void {graphics. clear (); graphics. linestyle (1, 0 xffffff); graphics. moveTo (10, 0); graphics. lineto (-10, 10); graphics. lineto (-5, 0); graphics. lineto (-10,-10); graphics. lineto (10, 0); If (showflame) {graphics. moveTo (-7.5,-5); graphics. lineto (-15,0); graphics. lineto (-7.5, 5 );}}}}

Active Image

Package {import flash. display. sprite; import flash. events. event; import flash. events. keyboardevent; import flash. UI. keyboard; public class shipsim extends sprite {private var ship: ship; private var Vr: Number = 0; private var thrust: Number = 0; private var VX: Number = 0; private var Vy: Number = 0; Public Function shipsim () {Init ();} private function Init (): void {ship = new ship; ship. scalex = ship. scale= 1.5; addchild (SHIP); ship. X = stage. stagewidth/2; ship. y = stage. stageheight/2; addeventlistener (event. enter_frame, enterframehandler); stage. addeventlistener (keyboardevent. key_down, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler);} private function keydownhandler (Event: keyboardevent): void {Switch (event. keycode) {Case keyboard. left: Vr =-5; break; Case keyboard. right: Vr = 5; break; Case keyboard. up: thrust = 0.2; ship. draw (true); break; default: break;} private function keyuphandler (Event: keyboardevent): void {Vr = 0; thrust = 0; ship. draw (false);} private function enterframehandler (Event: Event): void {ship. rotation + = VR; var angle: Number = ship. rotation * Math. PI/180; var ax: Number = math. cos (angle) * thrust; var ay: Number = math. sin (angle) * thrust; VX + = ax; Vy + = Ay; ship. X + = VX; ship. Y + = Vy; If (ship. x <ship. width/2) {ship. X = ship. width/2; vx = 0;} If (ship. x> stage. stagewidth-ship. width/2) {ship. X = stage. stagewidth-ship. width/2; vx = 0;} If (ship. y> stage. stageheight-ship. height/2) {ship. y = stage. stageheight-ship. height/2; Vy = 0;} If (ship. Y <ship. height/2) {ship. y = ship. height/2; Vy = 0 ;}}}}

Accelerated rotation with Friction:

 addeventlistener (event. enter_frame, enterframehandler); stage. addeventlistener (keyboardevent. key_down, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler); var AR: Number = 0; var Vr: Number = 0; VaR _ isstart: Boolean = false; // whether or not acceleration (or deceleration) VaR _ isright: boolean = true; // whether the function enterframehandler (E: Event) is rotating clockwise to the right: void {VR + = Ar; obj. rotation + = VR; // obj is the arrow in the stage Instance if (! _ Isstart) {If (_ isright) {If (VR <= 0) {Vr = 0; Ar = 0.0 ;}} else {If (VR> = 0) {Vr = 0; Ar = 0.0 ;}}} function keydownhandler (E: keyboardevent): void {_ isstart = true; If (E. keycode = keyboard. right) {AR = 0.2; _ isright = true;} else if (E. keycode = keyboard. left) {AR =-0.2; _ isright = false;} function keyuphandler (E: keyboardevent): void {If (E. keycode = keyboard. right) {AR =-0.1;} else if (E. keycode = keyboard. left) {AR = 0.1 ;}_ isstart = false ;}

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.