`
china34420
  • 浏览: 137953 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

弹性运动(二):二维弹性运动

阅读更多
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author zkl
	 */
	public class Spring1 extends Sprite 
	{
		private var ball:Ball;
		private var vx:Number = 0;
		private var vy:Number = 50;
		private var spring:Number = 0.1;
		private var friction:Number = 0.95;
		private var targetX:Number = stage.stageWidth / 2;
		private var targetY:Number = stage.stageHeight / 2;
		
		public function Spring1():void
		{
		    init();
		}
		private function init():void
		{
		    ball = new Ball;
			addChild(ball);
			//ball.y = stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, EnterFrame);
		}
		private function EnterFrame(e:Event):void
		{ 
			//与目标点的距离:
			var dx:Number = targetX - ball.x;
			var dy:Number = targetY - ball.y;
            //下面计算加速度。加速度与距离成正比,也就是距离乘以 spring 的值
			var ax:Number = dx * spring;
			var ay:Number = dy * spring;
            //把加速度加到速度向量中。
			vx += ax;
			vy += ay;
			//引入摩擦力
		    vx *= friction;
			vy *= friction;
			
			ball.x += vx;
			ball.y += vy;
		}
	}
}


最后加入重力,修改如下
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Graphics;
	/**
	 * ...引入y向速度后为二维弹性运动
	 * @author zkl
	 */
	public class Spring1 extends Sprite 
	{
		private var ball:Ball;
		private var vx:Number = 0;
		private var vy:Number = 20;
		private var spring:Number = 0.1;
		private var friction:Number = 0.95;
		//重力
		private var gravity:Number = 8;
		//private var targetX:Number = stage.stageWidth / 2;
		//private var targetY:Number = stage.stageHeight / 2;
		
		public function Spring1():void
		{
		    init();
		}
		private function init():void
		{
		    ball = new Ball;
			addChild(ball);
			//ball.y = stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, EnterFrame);
		}
		private function EnterFrame(e:Event):void
		{ 
			//与目标点的距离:
			var dx:Number = mouseX - ball.x;
			var dy:Number = mouseY - ball.y;
            //下面计算加速度。加速度与距离成正比,也就是距离乘以 spring 的值
			var ax:Number = dx * spring;
			var ay:Number = dy * spring;
            //把加速度加到速度向量中。
			vx += ax;
			vy += ay;
			vy += gravity;
			//引入摩擦力
		    vx *= friction;
			vy *= friction;
			
			ball.x += vx;
			ball.y += vy;
			
			drawLine(mouseX, mouseY, ball.x, ball.y);
		}
		private function drawLine(x1:Number,y1:Number,x2:Number,y2:Number):void
		{
		    graphics.clear();
			graphics.lineStyle(1);
			graphics.moveTo(x1, y1);
			graphics.lineTo(x2, y2);
		}
	}
	
}
分享到:
评论
2 楼 china34420 2010-08-09  
再画线绳子吧,哈哈代码修改如下
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Graphics;
	/**
	 * ...引入y向速度后为二维弹性运动
	 * @author zkl
	 */
	public class Spring1 extends Sprite 
	{
		private var ball:Ball;
		private var vx:Number = 0;
		private var vy:Number = 20;
		private var spring:Number = 0.1;
		private var friction:Number = 0.95;
		//private var targetX:Number = stage.stageWidth / 2;
		//private var targetY:Number = stage.stageHeight / 2;
		
		public function Spring1():void
		{
		    init();
		}
		private function init():void
		{
		    ball = new Ball;
			addChild(ball);
			//ball.y = stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, EnterFrame);
		}
		private function EnterFrame(e:Event):void
		{ 
			//与目标点的距离:
			var dx:Number = mouseX - ball.x;
			var dy:Number = mouseY - ball.y;
            //下面计算加速度。加速度与距离成正比,也就是距离乘以 spring 的值
			var ax:Number = dx * spring;
			var ay:Number = dy * spring;
            //把加速度加到速度向量中。
			vx += ax;
			vy += ay;
			//引入摩擦力
		    vx *= friction;
			vy *= friction;
			
			ball.x += vx;
			ball.y += vy;
			
			drawLine(mouseX, mouseY, ball.x, ball.y);
		}
		private function drawLine(x1:Number,y1:Number,x2:Number,y2:Number):void
		{
		    graphics.clear();
			graphics.lineStyle(1);
			graphics.moveTo(x1, y1);
			graphics.lineTo(x2, y2);
		}
	}

}
1 楼 china34420 2010-08-09  

var dx:Number = targetX - ball.x;  
var dy:Number = targetY - ball.y; 

改为再试一试,挺cool的
var dx:Number = mouseX - ball.x;
var dy:Number = mouseY - ball.y;

相关推荐

Global site tag (gtag.js) - Google Analytics