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

对象拖拽:

这其实就是以前所学知识:Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag) + Flash/Flex学习笔记(23):运动学原理 的综合运用,要提一下的是下面代码中对于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;//反弹速度百分比
		private var gravity:Number=0.9;//重力加速度百分比
		private var frictionX:Number=0.98;//摩擦力因子--水平方向
		private var frictionY:Number=0.99;//摩擦力因子--垂直方向
		
		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; //加入重力加速度,所以肯定会向下掉
			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;
			}
		}
		private function MouseDownHandler(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);
			ball.startDrag();
			removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);//移除EnterFrame事件,否则在拖动过程中,小球还在不断下掉
		}
		private function MouseUpHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);
			ball.stopDrag();
			addEventListener(Event.ENTER_FRAME,EnterFrameHandler);//拖动完成后,恢复EnterFrame事件监听,以便小球能不断运动
		}
	}
}

对象投掷:

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

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;
			}
		}
		
		//跟踪每一帧小球的速度(坐标位置)
		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);
		}
		
		
	}
}

你可能感兴趣的:(Flash)