对象拖拽:
这其实就是以前所学知识:Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag) +Flash/Flex学习笔记(23):运动学原理 的综合运用,要提一下的是下面代码中对于EnterFrame的添加与移除操作
02 |
import flash.display.Sprite; |
03 |
import flash.display.StageAlign; |
04 |
import flash.display.StageScaleMode; |
05 |
import flash.events.Event; |
06 |
import flash.events.MouseEvent; |
07 |
import flash.ui.Mouse; |
08 |
import flash.ui.MouseCursor; |
10 |
public class Bouncing2 extends Sprite { |
12 |
private var ball:Ball; |
13 |
private var vx: Number ; |
14 |
private var vy: Number ; |
15 |
private var bounce: Number =- 0.8 ; //反弹速度百分比 |
16 |
private var gravity: Number = 0.9 ; //重力加速度百分比 |
17 |
private var frictionX: Number = 0.98 ; //摩擦力因子--水平方向 |
18 |
private var frictionY: Number = 0.99 ; //摩擦力因子--垂直方向 |
20 |
public function Bouncing2() { |
24 |
private function init(): void { |
25 |
stage.scaleMode=StageScaleMode.NO_SCALE; |
26 |
stage.align=StageAlign.TOP_LEFT; |
28 |
ball.x=stage.stageWidth/ 2 ; |
29 |
ball.y=stage.stageHeight/ 2 ; |
30 |
vx=(Math.random()* 2 - 1 ) * 20 ; |
33 |
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler); |
34 |
ball.addEventListener(MouseEvent.MOUSE_OVER, function (){ Mouse.cursor = MouseCursor.HAND;}); |
35 |
ball.addEventListener(MouseEvent.MOUSE_OUT, function (){ Mouse.cursor = MouseCursor.AUTO;}); |
36 |
addEventListener(Event.ENTER_FRAME,EnterFrameHandler); |
38 |
private function EnterFrameHandler(event:Event): void { |
39 |
vy+=gravity; //加入重力加速度,所以肯定会向下掉 |
40 |
vx *= frictionX; //加入摩擦力,所以最终会停下来 |
46 |
var right: Number =stage.stageWidth; |
48 |
var bottom: Number =stage.stageHeight; |
51 |
if (ball.x+ball.radius>right) { |
52 |
ball.x=right-ball.radius; |
54 |
} else if (ball.x - ball.radius < left) { |
55 |
ball.x=left+ball.radius; |
60 |
if (ball.y+ball.radius>bottom) { |
61 |
ball.y=bottom-ball.radius; |
63 |
} else if (ball.y - ball.radius < top) { |
64 |
ball.y=top+ball.radius; |
68 |
private function MouseDownHandler(e:MouseEvent): void { |
69 |
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler); |
71 |
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler); //移除EnterFrame事件,否则在拖动过程中,小球还在不断下掉 |
73 |
private function MouseUpHandler(e:MouseEvent): void { |
74 |
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUpHandler); |
76 |
addEventListener(Event.ENTER_FRAME,EnterFrameHandler); //拖动完成后,恢复EnterFrame事件监听,以便小球能不断运动 |
对象投掷:
在上面的代码中,鼠标的拖拽只影响了小球的y坐标(注:指对运动方面的影响),即仅相当于举高了小球。而投掷则意味着:在鼠标松开小球的那一瞬间,小球也应该具备有一定的出口速度(即鼠标最终的移动速度)。Flash中每一帧对应的时间基本上是相同的,可以认为这就是物理中的“单位时间”,根据刚才对投掷概念的理解,只要代码能得知在鼠标松开小球的最后一帧,小球在x,y轴方向的位移,即为小球的出口x,y轴速度.
02 |
import flash.display.Sprite; |
03 |
import flash.display.StageAlign; |
04 |
import flash.display.StageScaleMode; |
05 |
import flash.events.Event; |
06 |
import flash.events.MouseEvent; |
07 |
import flash.ui.MouseCursor; |
08 |
import flash.ui.Mouse; |
10 |
public class Throwing extends Sprite { |
12 |
private var ball:Ball; |
13 |
private var vx: Number ; |
14 |
private var vy: Number ; |
15 |
private var bounce: Number =- 0.8 ; |
16 |
private var gravity: Number = 0.75 ; |
17 |
private var frictionX: Number = 0.98 ; |
18 |
private var frictionY: Number = 0.99 ; |
19 |
private var oldX: Number ; |
20 |
private var oldY: Number ; |
22 |
public function Throwing() { |
26 |
private function init(): void { |
27 |
stage.scaleMode=StageScaleMode.NO_SCALE; |
28 |
stage.align=StageAlign.TOP_LEFT; |
30 |
ball.x = stage.stageWidth/ 2 ; |
31 |
ball.y = stage.stageHeight/ 2 ; |
32 |
vx = Math.random()* 10 - 5 ; |
35 |
ball.addEventListener(MouseEvent.MOUSE_DOWN, MouseDownHandler); |
36 |
ball.addEventListener(MouseEvent.MOUSE_OVER, function (){Mouse.cursor = MouseCursor.HAND;}); |
37 |
ball.addEventListener(MouseEvent.MOUSE_OUT, function (){Mouse.cursor = MouseCursor.AUTO;}); |
38 |
addEventListener(Event.ENTER_FRAME, EnterFrameHandler); |
41 |
private function MouseDownHandler(event:MouseEvent): void { |
44 |
stage.addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler); |
46 |
removeEventListener(Event.ENTER_FRAME, EnterFrameHandler); |
47 |
addEventListener(Event.ENTER_FRAME, TrackVelocity); |
50 |
private function EnterFrameHandler(event:Event): void { |
57 |
var right: Number =stage.stageWidth; |
59 |
var bottom: Number =stage.stageHeight; |
60 |
if (ball.x+ball.radius>right) { |
61 |
ball.x=right-ball.radius; |
63 |
} else if (ball.x - ball.radius < left) { |
65 |
ball.x=left+ball.radius; |
68 |
if (ball.y+ball.radius>bottom) { |
69 |
ball.y=bottom-ball.radius; |
71 |
} else if (ball.y - ball.radius < top) { |
72 |
ball.y=top+ball.radius; |
78 |
private function TrackVelocity(event:Event): void { |
85 |
private function MouseUpHandler(e:MouseEvent): void { |
86 |
stage.removeEventListener(MouseEvent.MOUSE_UP, MouseUpHandler); |
88 |
removeEventListener(Event.ENTER_FRAME, TrackVelocity); |
89 |
addEventListener(Event.ENTER_FRAME, EnterFrameHandler); |