Flash/Flex学习笔记(31):对象拖拽与投掷

Flash/Flex学习笔记(31):对象拖拽与投掷

对象拖拽:

这其实就是以前所学知识:Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag) +Flash/Flex学习笔记(23):运动学原理 的综合运用,要提一下的是下面代码中对于EnterFrame的添加与移除操作

01 package {
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;
09      
10     public class Bouncing2 extends Sprite {
11          
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;//摩擦力因子--垂直方向
19          
20         public function Bouncing2() {
21             init();
22         }
23          
24         private function init():void {
25             stage.scaleMode=StageScaleMode.NO_SCALE;
26             stage.align=StageAlign.TOP_LEFT;
27             ball=new Ball(20)  ;
28             ball.x=stage.stageWidth/2;
29             ball.y=stage.stageHeight/2;
30             vx=(Math.random()*2-1) * 20;
31             vy=-10;
32             addChild(ball);
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);
37         }
38         private function EnterFrameHandler(event:Event):void {
39             vy+=gravity; //加入重力加速度,所以肯定会向下掉
40             vx *= frictionX; //加入摩擦力,所以最终会停下来
41             vy *= frictionY;
42             ball.x+=vx;//产生移动
43             ball.y+=vy;
44              
45             var left:Number=0;
46             var right:Number=stage.stageWidth;
47             var top:Number=0;
48             var bottom:Number=stage.stageHeight;
49              
50             //水平方向边界检测
51             if (ball.x+ball.radius>right) {
52                 ball.x=right-ball.radius;
53                 vx*=bounce;
54             else if (ball.x - ball.radius < left) {
55                 ball.x=left+ball.radius;
56                 vx*=bounce;
57             }
58              
59             //垂直方向边界检测
60             if (ball.y+ball.radius>bottom) {
61                 ball.y=bottom-ball.radius;
62                 vy*=bounce;
63             else if (ball.y - ball.radius < top) {
64                 ball.y=top+ball.radius;
65                 vy*=bounce;
66             }
67         }
68         private function MouseDownHandler(e:MouseEvent):void {
69             stage.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);
70             ball.startDrag();
71             removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);//移除EnterFrame事件,否则在拖动过程中,小球还在不断下掉
72         }
73         private function MouseUpHandler(e:MouseEvent):void {
74             stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);
75             ball.stopDrag();
76             addEventListener(Event.ENTER_FRAME,EnterFrameHandler);//拖动完成后,恢复EnterFrame事件监听,以便小球能不断运动
77         }
78     }
79 }

对象投掷:

在上面的代码中,鼠标的拖拽只影响了小球的y坐标(注:指对运动方面的影响),即仅相当于举高了小球。而投掷则意味着:在鼠标松开小球的那一瞬间,小球也应该具备有一定的出口速度(即鼠标最终的移动速度)。Flash中每一帧对应的时间基本上是相同的,可以认为这就是物理中的“单位时间”,根据刚才对投掷概念的理解,只要代码能得知在鼠标松开小球的最后一帧,小球在x,y轴方向的位移,即为小球的出口x,y轴速度.

01 package {
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;
09      
10     public class Throwing extends Sprite {
11          
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;
21          
22         public function Throwing() {
23             init();
24         }
25          
26         private function init():void {
27             stage.scaleMode=StageScaleMode.NO_SCALE;
28             stage.align=StageAlign.TOP_LEFT;
29             ball = new Ball(30);
30             ball.x = stage.stageWidth/2;
31             ball.y = stage.stageHeight/2;
32             vx = Math.random()*10-5;
33             vy = -10;
34             addChild(ball);
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);
39         }
40          
41         private function MouseDownHandler(event:MouseEvent):void {
42             oldX = ball.x;
43             oldY = ball.y;
44             stage.addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
45             ball.startDrag();
46             removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
47             addEventListener(Event.ENTER_FRAME, TrackVelocity);
48         }
49          
50         private function EnterFrameHandler(event:Event):void {
51             vy += gravity;
52             vx *= frictionX;
53             vy *= frictionY;
54             ball.x += vx;
55             ball.y += vy;
56             var left:Number=0;
57             var right:Number=stage.stageWidth;
58             var top:Number=0;
59             var bottom:Number=stage.stageHeight;
60             if (ball.x+ball.radius>right) {
61                 ball.x=right-ball.radius;
62                 vx*=bounce;
63             else if (ball.x - ball.radius < left) {
64                  
65                 ball.x=left+ball.radius;
66                 vx*=bounce;
67             }
68             if (ball.y+ball.radius>bottom) {
69                 ball.y=bottom-ball.radius;
70                 vy*=bounce;
71             else if (ball.y - ball.radius < top) {
72                 ball.y=top+ball.radius;
73                 vy*=bounce;
74             }
75         }
76          
77         //跟踪每一帧小球的速度(坐标位置)
78         private function TrackVelocity(event:Event):void {
79             vx = ball.x-oldX;
80             vy = ball.y-oldY;
81             oldX = ball.x;
82             oldY = ball.y;
83         }
84          
85         private function MouseUpHandler(e:MouseEvent):void {
86             stage.removeEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
87             ball.stopDrag();
88             removeEventListener(Event.ENTER_FRAME, TrackVelocity);
89             addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
90         }
91          
92          
93     }
94 }
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted on 2010-11-17 16:40 aiaiwoo 阅读(95) 评论(0)  编辑  收藏 所属分类: AC3/FLEX


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜