package com.ddd
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Easing3D extends Sprite
{
private var ball:Ball;
private var tx:Number=0;
private var ty:Number=0;
private var tz:Number=0;
private var vpX:Number=stage.stageWidth / 2;
private var vpY:Number=stage.stageHeight / 2;
//缓动系数
private var easing:Number=.08;
//弹性系数 一般比缓动系数大,但都小于1
private var fir:Number=.6;
private var fl:Number=250;
public function Easing3D()
{
super();
init();
}
private function init():void
{
ball=new Ball;
this.addChild(ball);
tx=Math.random() * 500 - 250;
ty=Math.random() * 500 - 250;
tz=Math.random() * 500;
this.addEventListener(Event.ENTER_FRAME, onEnterFrame)
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
}
private function onMouseDown(e:MouseEvent):void
{
tx=Math.random() * 500 - 250;
ty=Math.random() * 500 - 250;
tz=Math.random() * 500;
}
private function onEnterFrame(e:Event):void
{
var dx:Number=tx - ball.xpos;
var dy:Number=ty - ball.ypos;
var dz:Number=tz - ball.zpos;
dx*=easing;
dy*=easing;
dz*=easing;
ball.vx+=dx;
ball.vy+=dy;
ball.vz+=dz;
ball.xpos+=ball.vx;
ball.ypos+=ball.vy;
ball.zpos+=ball.vz;
ball.vx*=fir;
ball.vy*=fir;
ball.vz*=fir;
if (ball.zpos > -fl)
{
var scale:Number=fl / (fl + ball.zpos);
ball.scaleX=ball.scaleY=scale;
ball.x=vpX + ball.xpos * scale;
ball.y=vpY + ball.ypos * scale;
ball.visible=true;
}
else
{
ball.visible=false;
}
}
}
}