Drag an object:
This is actually the previous knowledge: Flash/flex learning notes (13): Object dragging (startdrag/stopdrag) + flash/flex learning notes (23): comprehensive application of the kinematics principle, which of the following is a reference?CodeAdding and removing enterframe
Package {import flash. display. sprite; import flash. display. stagealign; import flash. display. stagescalemode; import flash. events. event; import flash. events. mouseevent; import flash. UI. mouse; import flash. UI. mousecursor; public class bouncing2 extends sprite {private var ball: ball; private var VX: Number; private var Vy: Number; private var bounce: Number =-0.8; // percentage of bounce speed private var gravity: Number = 0.9; // percentage of gravity acceleration private var frictionx: Number = 0.98; // friction factor-horizontal private var frictiony: number = 0.99; // friction factor -- vertical direction public function bouncing2 () {Init ();} private function Init (): void {stage. scalemode = stagescalemode. no_scale; stage. align = stagealign. top_left; ball = new ball (20); ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; vx = (math. random () * 2-1) * 20; Vy =-10; addchild (ball); ball. addeventlistener (mouseevent. mouse_down, mousedownhandler); ball. addeventlistener (mouseevent. mouse_over, function () {mouse. cursor = mousecursor. hand;}); ball. addeventlistener (mouseevent. mouse_out, function () {mouse. cursor = mousecursor. auto;}); addeventlistener (event. enter_frame, enterframehandler);} private function enterframehandler (Event: Event): void {Vy + = gravity; // Add gravity acceleration, so VX * = frictionx will definitely be dropped down; // Add the friction, so the Vy * = frictiony; ball will be stopped. X + = VX; // generates a moving ball. Y + = Vy; var left: Number = 0; var right: Number = stage. stagewidth; var top: Number = 0; var bottom: Number = stage. stageheight; // horizontal Border Detection If (ball. X + ball. radius> right) {ball. X = right-ball.radius; VX * = bounce;} else if (ball. x-ball. radius <left) {ball. X = left + ball. radius; VX * = bounce;} // If (ball. Y + ball. radius> bottom) {ball. y = bottom-ball.radius; Vy * = bounce;} else if (ball. y-ball. radius <top) {ball. y = Top + ball. radius; Vy * = bounce;} private function mousedownhandler (E: mouseevent): void {stage. addeventlistener (mouseevent. mouse_up, mouseuphandler); ball. startdrag (); removeeventlistener (event. enter_frame, enterframehandler); // remove the enterframe event; otherwise, the ball is still being dragged.} private function mouseuphandler (E: mouseevent): void {stage. removeeventlistener (mouseevent. mouse_up, mouseuphandler); ball. stopdrag (); addeventlistener (event. enter_frame, enterframehandler); // After dragging, resume the enterframe event listening so that the ball can be continuously moved }}}
Object throwing:
In the above Code, the drag and drop of the mouse only affects the Y coordinate of the ball (Note: it refers to the impact on motion), that is, it is only equivalent to raising the ball. Throw means that the ball should have a certain exit speed (that is, the final moving speed of the mouse) when the mouse releases the ball ). The time corresponding to each frame in Flash is basically the same. It can be considered as "unit time" in physics. According to the previous understanding of the throwing concept, as long as the code can tell that the last frame of the ball is released with the mouse, the displacement of the ball in the direction of X and Y is the exit X and Y axis speed of the ball.
Package {import flash. display. sprite; import flash. display. stagealign; import flash. display. stagescalemode; import flash. events. event; import flash. events. mouseevent; import flash. UI. mousecursor; import flash. UI. mouse; public class throwing extends sprite {private var ball: ball; private var VX: Number; private var Vy: Number; private var bounce: Number =-0.8; private var gravity: number = 0.75; private var frictionx: Number = 0.98; private var frictiony: Number = 0.99; private var oldx: Number; private var Oldy: Number; Public Function throwing () {Init ();} private function Init (): void {stage. scalemode = stagescalemode. no_scale; stage. align = stagealign. top_left; ball = new ball (30); ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; vx = math. random () * 10-5; Vy =-10; addchild (ball); ball. addeventlistener (mouseevent. mouse_down, mousedownhandler); ball. addeventlistener (mouseevent. mouse_over, function () {mouse. cursor = mousecursor. hand;}); ball. addeventlistener (mouseevent. mouse_out, function () {mouse. cursor = mousecursor. auto;}); addeventlistener (event. enter_frame, enterframehandler);} private function mousedownhandler (Event: mouseevent): void {oldx = ball. x; Oldy = ball. y; stage. addeventlistener (mouseevent. mouse_up, mouseuphandler); ball. startdrag (); removeeventlistener (event. enter_frame, enterframehandler); addeventlistener (event. enter_frame, trackvelocity);} private function enterframehandler (Event: Event): void {Vy + = gravity; VX * = frictionx; Vy * = frictiony; ball. X + = VX; ball. Y + = Vy; var left: Number = 0; var right: Number = stage. stagewidth; var top: Number = 0; var bottom: Number = stage. stageheight; If (ball. X + ball. radius> right) {ball. X = right-ball.radius; VX * = bounce;} else if (ball. x-ball. radius <left) {ball. X = left + ball. radius; VX * = bounce;} If (ball. Y + ball. radius> bottom) {ball. y = bottom-ball.radius; Vy * = bounce;} else if (ball. y-ball. radius <top) {ball. y = Top + ball. radius; Vy * = bounce;} // tracks the speed (Coordinate Position) of each frame of the ball. Private function trackvelocity (Event: Event): void {vx = ball. x-oldx; Vy = ball. y-Oldy; oldx = ball. x; Oldy = ball. y;} private function mouseuphandler (E: mouseevent): void {stage. removeeventlistener (mouseevent. mouse_up, mouseuphandler); ball. stopdrag (); removeeventlistener (event. enter_frame, trackvelocity); addeventlistener (event. enter_frame, enterframehandler );}}}