SpringEarthShakeEffectTest类:代码+注释:
下载运行实例:SpringEarthShakeEffectTest.rar
package
{
import controlsEvents.DirectionPickerEvent;
import controlsEvents.NumericeUpDownEvent;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.net.getClassByAlias;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
import flash.utils.getTimer;
/**
* 弹性振动效果
* @author Jave.Lin
*/
public class SpringEarthShakeEffectTest extends Sprite
{
private var _sp:Sprite;
private var _frictionX:Number=.3;
private var _frictionY:Number=.3;
private var _springX:Number=3;
private var _springY:Number=3;
private var _earthMass:Number=60;//
private var _hitterMass:Number=100;
private var _vx:Number=0;
private var _vy:Number=0;
private var _spSourceX:Number=0;
private var _spSourceY:Number=0;
private var _mc:MovieClip;
private var _amplitude:Number=100;//振幅
private var _duration:Number=100;//单位:毫秒
private var _startShakeTime:Number=0;//开始振动的计时
private var _shakeDirection:Number=0;//振动方向,单位:度(0~360)
private var _shakeOffsetX:Number=2;//X方向的振动抖动偏移量
private var _shakeOffsetY:Number=2;//Y方向的振动抖动偏移量
private var _directionNUD:NumericUpDown;
[SWF(width="1200",height="720")]
public function SpringEarthShakeEffectTest()
{
super();
stage.frameRate=60;
stage.color=0xccccaa;
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
_sp=new Sprite();
addChild(_sp);
// _sp.graphics.beginFill(0xff0000);
// _sp.graphics.drawCircle(0,0,20);
// _sp.graphics.endFill();
//
// _spSourceX=_sp.x=200;
// _spSourceY=_sp.y=100;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownHandler);
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onPicLoadedHandler);
loader.load(new URLRequest("res/test1.jpg"),new LoaderContext(false, ApplicationDomain.currentDomain));
var loader3:Loader=new Loader();
loader3.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwf3LoadedHandler);
loader3.load(new URLRequest("res/vDropball.swf"),new LoaderContext(false, ApplicationDomain.currentDomain));
var durationLable:Label=new Label();
addChild(durationLable);
durationLable.fontSize=15;
durationLable.text="振动持续时间:(100~5000)毫秒";
durationLable.x=100;
durationLable.y=100;
var durationNumericeUpDown:NumericUpDown=new NumericUpDown();
addChild(durationNumericeUpDown);
durationNumericeUpDown.fontColor=0;
durationNumericeUpDown.maxValue=5000;
durationNumericeUpDown.minValue=100;
durationNumericeUpDown.x=durationLable.x+durationLable.width;
durationNumericeUpDown.y=durationLable.y;
durationNumericeUpDown.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,ondurationNumericeUpDownValueChange);
durationNumericeUpDown.curValue=100;
var shakAmplitudeLable:Label=new Label();
addChild(shakAmplitudeLable);
shakAmplitudeLable.fontSize=15;
shakAmplitudeLable.text="振幅:(100~2000)";
shakAmplitudeLable.x=100;
shakAmplitudeLable.y=120;
var shakAmplitudeNumericeUpDown:NumericUpDown=new NumericUpDown();
addChild(shakAmplitudeNumericeUpDown);
shakAmplitudeNumericeUpDown.fontColor=0;
shakAmplitudeNumericeUpDown.minValue=100;
shakAmplitudeNumericeUpDown.maxValue=2000;
shakAmplitudeNumericeUpDown.x=shakAmplitudeLable.x+shakAmplitudeLable.width;
shakAmplitudeNumericeUpDown.y=shakAmplitudeLable.y;
shakAmplitudeNumericeUpDown.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onshakAmplitudeNumericeUpDownValueChange);
shakAmplitudeNumericeUpDown.curValue=100;
var scaleXYLable:Label=new Label();
addChild(scaleXYLable);
scaleXYLable.fontSize=15;
scaleXYLable.text="振动对象缩放比率:(0.1~1)";
scaleXYLable.x=100;
scaleXYLable.y=140;
var scaleXYNUD:NumericUpDown=new NumericUpDown();
addChild(scaleXYNUD);
scaleXYNUD.fontColor=0;
scaleXYNUD.minValue=0.1;
scaleXYNUD.maxValue=1;
scaleXYNUD.increment=0.01;
scaleXYNUD.x=scaleXYLable.x+scaleXYLable.width;
scaleXYNUD.y=scaleXYLable.y;
scaleXYNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onscaleXYNUDNumericeUpDownValueChange);
scaleXYNUD.curValue=100;
var sourceXLable:Label=new Label();
addChild(sourceXLable);
sourceXLable.fontSize=15;
sourceXLable.text="振动对象x:(-1200~1200)";
sourceXLable.x=100;
sourceXLable.y=160;
var sourceXNUD:NumericUpDown=new NumericUpDown();
addChild(sourceXNUD);
sourceXNUD.fontColor=0;
sourceXNUD.minValue=-1200;
sourceXNUD.maxValue=1200;
sourceXNUD.increment=5;
sourceXNUD.x=sourceXLable.x+sourceXLable.width;
sourceXNUD.y=sourceXLable.y;
sourceXNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onsourceXNUDNumericeUpDownValueChange);
sourceXNUD.curValue=100;
var sourceYLable:Label=new Label();
addChild(sourceYLable);
sourceYLable.fontSize=15;
sourceYLable.text="振动对象y:(-720~720)";
sourceYLable.x=100;
sourceYLable.y=180;
var sourceYNUD:NumericUpDown=new NumericUpDown();
addChild(sourceYNUD);
sourceYNUD.fontColor=0;
sourceYNUD.minValue=-720;
sourceYNUD.maxValue=720;
sourceYNUD.increment=5;
sourceYNUD.x=sourceYLable.x+sourceYLable.width;
sourceYNUD.y=sourceYLable.y;
sourceYNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onsourceYNUDNumericeUpDownValueChange);
sourceYNUD.curValue=100;
var directionLable:Label=new Label();
addChild(directionLable);
directionLable.fontSize=15;
directionLable.text="振动方向:(0~360)";
directionLable.x=100;
directionLable.y=200;
var directionPicker:DirectionPicker=new DirectionPicker();
addChild(directionPicker);
directionLable.y+=directionPicker.radius;
directionPicker.x=directionLable.x+directionLable.width+directionPicker.radius+10;
directionPicker.y=directionLable.y+directionPicker.radius/2;
directionPicker.addEventListener(DirectionPickerEvent.DEGREE_CHANGED,ondirectionPickerDegreeChanged);
_directionNUD=new NumericUpDown();
addChild(_directionNUD);
_directionNUD.fontColor=0;
_directionNUD.minValue=0;
_directionNUD.maxValue=360;
_directionNUD.x=directionPicker.x+directionPicker.radius+10;
_directionNUD.y=directionPicker.y-_directionNUD.height/2;
_directionNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,ondirectionNUDValueChanged);
directionPicker.degree=_directionNUD.curValue=1;
directionPicker.degree=_directionNUD.curValue=0;
var directionLb:Label=new Label("度");
addChild(directionLb);
directionLb.fontSize=15;
directionLb.x=_directionNUD.x+_directionNUD.width;
directionLb.y=_directionNUD.y;
}
private function ondirectionNUDValueChanged(e:NumericeUpDownEvent):void
{
_shakeDirection=e.value;
}
private function ondirectionPickerDegreeChanged(e:DirectionPickerEvent):void
{
_directionNUD.curValue=e.degree;
}
private function onsourceYNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_spSourceY=_sp.y=e.value;
}
private function onsourceXNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_spSourceX=_sp.x=e.value;
}
private function onscaleXYNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_sp.scaleX=_sp.scaleY=e.value;
}
private function onshakAmplitudeNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_amplitude=e.value;
}
private function ondurationNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_duration=e.value;
}
private function onSwf3LoadedHandler(e:Event):void
{
_mc=e.currentTarget.content as MovieClip;
}
private function onPicLoadedHandler(e:Event):void
{
var bmp:Bitmap=e.currentTarget.content as Bitmap;
_sp.addChild(bmp);
_sp.setChildIndex(bmp,0);
}
private function onKeyDownHandler(e:KeyboardEvent):void
{
// trace(e.keyCode);//32 == sapce key
if(e.keyCode==32)
{
//shake
// 弹性公式:
// vx += (targetX - sprite.x) * spring;//spring为弹性系数
// vy += (targetY - sprite.y) * spring;
// sprite.x += (vx *= friction);//friction为摩擦力
// sprite.y += (vy *= friction);
var c:Class=ApplicationDomain.currentDomain.getDefinition("vDropBall") as Class;
var mc:MovieClip=new c() as MovieClip;
mc.x=Math.random()*300+100;mc.y=Math.random()*400+100;
mc.addFrameScript(mc.totalFrames-1,
function():void
{
mc.addFrameScript(mc.totalFrames-1,null);
_sp.removeChild(mc);
mc=null;
shake(_duration);
});
_sp.addChild(mc);
_sp.setChildIndex(mc,_sp.numChildren-1);
}
}
private function shake(duration:Number):void
{
_startShakeTime=getTimer();
_duration=duration;
if(!hasEventListener(Event.ENTER_FRAME))addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
private function clamp(value:Number,min:Number,max:Number):Number
{
if(value<min)value=min;
else if(value>max)value=max;
return value;
}
private function onEnterFrameHandler(e:Event):void
{
trace("get vx,vy",_vx,_vy);
if((getTimer()-_startShakeTime)<_duration)
{
// trace("(getTimer()-_startTime)",(getTimer()-_startTime));
var offsetX:Number=Math.random()*_shakeOffsetX-(_shakeOffsetX/2);
var offsetY:Number=Math.random()*_shakeOffsetY-(_shakeOffsetY/2);
var p:Point=Point.polar(_amplitude,_shakeDirection*(Math.PI/180));
var vx0:Number=Math.random()*p.x+(p.x/2);
var vy0:Number=Math.random()*p.y+(p.y/2);
//v=((m0-m1)*v0+(2*m1*v1))/(m0+m1);
_vx+=((_hitterMass-_earthMass)*vx0+(2*_earthMass*_vx))/(_hitterMass+_earthMass);
_vy+=((_hitterMass-_earthMass)*vy0+(2*_earthMass*_vy))/(_hitterMass+_earthMass);
// _vy=Math.sin(Math.PI/4)*_vy;//透视,俯视45度
// _vy=.25*_vy;//透视,俯视45度
_vx+=offsetX;//加上抖动偏移量
_vy+=offsetY;
var waveX:Number=Math.abs(p.x/2);
var waveY:Number=Math.abs(p.y/2);
_vx=clamp(_vx,-waveX,waveX);
_vy=clamp(_vy,-waveY,waveY);
_sp.x=_spSourceX+_vx;
_sp.y=_spSourceY+_vy;
}
var dx:Number=_spSourceX - _sp.x;
var dy:Number=_spSourceY -_sp.y;
_vx+=dx*_springX;
_vy+=dy*_springY;
_vx*=_frictionX;
_vy*=_frictionY;
_sp.x+=_vx;
_sp.y+=_vy;
if(Math.abs(dx)<0.1)_sp.x=_spSourceX;
if(Math.abs(dy)<0.1)_sp.y=_spSourceY;
if(dx==0&&dy==0)
{
// trace("complete");
removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
}
}
}
把振动效果封装成,工具类:Utils.as
package
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Dictionary;
import flash.utils.getTimer;
public class Utils
{
public static const RandomDirecitonShake:String="RandomDirecitonShake";//随机方向振动
public static const SpecialDirecitonShake:String="SpecialDirecitonShake";//指定方向振动
public static const AngleUnit:Number=Math.PI/180;
public static const DegreeUnit:Number=180/Math.PI;
public function Utils()
{
}
private static const SC_SHAKE_DATA_DIC:Dictionary=new Dictionary();
/**
* 振动指定的显示对象
*
*
* 演示:
*
*
* var disObj:DisplayObject=...;//显示对象
* var duration:Number=500;//持续500毫秒的时间
* var amplitude:Number=100;//振幅:100
* var shakeModel:String=Utils.SpecialDirecitonShake;//振动方式:指定方向
* var shakeDirection:Number=90;//单位:度,往垂直向下的方向振动
* var recoverX:Number=0;//振动完后恢复的X,Y位置
* var recoverY:Number=0;
* var shakeOffsetX:Number=2;//振动时的X,Y方向偏移动
* var shakeOffsetY:Number=2;
* //调用振动API
* Utils.shake(disObj,duration,amplitude,shakeModel,shakeDirection,recoverX,recoverY,shakeOffsetX,shakeOffsetY);
*
*
* @param disObj 指定显示对象
* @param duration 持续时间
* @param amplitude 振幅
* @param directionModel 振动模式(随机方向振动:ShakeUtils.RandomDirecitonShake;指定方向振动:ShakeUtils.SpecialDirecitonShake)
* @param direction 振动方向(当振动模式为:随机模式时,该值可以是任意值)
* @param recoverX 振动后,恢复的X位置,默认为0
* @param recoverY 振动后,恢复的Y位置,默认为0
* @param shakeOffsetX 振动时的X方向偏移动,默认为2
* @param shakeOffsetY 振动时的Y方向偏移动,默认为2
* @example 下面例子是通过print函数输出信息。
*
* var disObj:DisplayObject=...;//显示对象
* var duration:Number=500;//持续500毫秒的时间
* var amplitude:Number=100;//振幅:100
* var shakeModel:String=Utils.SpecialDirecitonShake;//振动方式:指定方向
* var shakeDirection:Number=90;//单位:度,往垂直向下的方向振动
* var recoverX:Number=0;//振动完后恢复的X,Y位置
* var recoverY:Number=0;
* var shakeOffsetX:Number=2;//振动时的X,Y方向偏移动
* var shakeOffsetY:Number=2;
* //调用振动API
* Utils.shake(disObj,duration,amplitude,shakeModel,shakeDirection,recoverX,recoverY,shakeOffsetX,shakeOffsetY);
*
*/
public static function shake(
disObj:DisplayObject,
duration:Number,
amplitude:Number,
directionModel:String,
direction:Number,
recoverX:Number=0,
recoverY:Number=0,
shakeOffsetX:Number=2,
shakeOffsetY:Number=2
):void
{
var data:Object;
if(SC_SHAKE_DATA_DIC.hasOwnProperty(disObj))
{
data=SC_SHAKE_DATA_DIC[disObj];
}
else
{
SC_SHAKE_DATA_DIC[disObj]=data=new Object();
}
data.disObj=disObj;
data.duration=duration;
data.amplitude=amplitude;
data.directionModel=directionModel;
data.direction=direction;
data.recoverX=recoverX;
data.recoverY=recoverY;
data.shakeOffsetX=shakeOffsetX;
data.shakeOffsetY=shakeOffsetY;
data.startShakeTime=getTimer();
data.vx=data.vx?data.vx:0;
data.vy=data.vy?data.vy:0;
disObj.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
disObj.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
private static function clamp(value:Number,min:Number,max:Number):Number
{
if(value<min)value=min;
else if(value>max)value=max;
return value;
}
private static function onEnterFrameHandler(e:Event):void
{
var disObj:DisplayObject=e.target as DisplayObject;
var data:Object=SC_SHAKE_DATA_DIC[disObj];
var duration:Number=data.duration;
var amplitude:Number=data.amplitude;
var directionModel:String=data.directionModel;
var direction:Number=data.direction;
var recoverX:Number=data.recoverX;
var recoverY:Number=data.recoverY;
var shakeOffsetX:Number=data.shakeOffsetX;
var shakeOffsetY:Number=data.shakeOffsetY;
var vx:Number=data.vx;
var vy:Number=data.vy;
if((getTimer()-data.startShakeTime)<duration)
{
var offsetX:Number=Math.random()*shakeOffsetX-(shakeOffsetX/2);
var offsetY:Number=Math.random()*shakeOffsetY-(shakeOffsetY/2);
var p:Point;
if(directionModel==SpecialDirecitonShake)
{
p=Point.polar(amplitude,direction*AngleUnit);
}
else
{
p=Point.polar(amplitude,Math.random()*360*AngleUnit);
}
var vx0:Number=Math.random()*p.x+(p.x/2);
var vy0:Number=Math.random()*p.y+(p.y/2);
// vx+=((100-60)*vx0+(2*60*vx))/(100+60);
// vy+=((100-60)*vy0+(2*60*vy))/(100+60);
vx+=(40*vx0+(120*vx))/160;
vy+=(40*vy0+(120*vy))/160;
// _vy=.25*_vy;//透视,俯视45度
vx+=offsetX;//加上抖动偏移量
vy+=offsetY;
var waveX:Number=Math.abs(p.x/2);
var waveY:Number=Math.abs(p.y/2);
vx=clamp(vx,-waveX,waveX);
vy=clamp(vy,-waveY,waveY);
disObj.x=recoverX+vx;
disObj.y=recoverY+vy;
}
var dx:Number=recoverX - disObj.x;
var dy:Number=recoverY -disObj.y;
vx+=dx*3;
vy+=dy*3;
vx*=.3;
vy*=.3;
disObj.x+=vx;
disObj.y+=vy;
if(Math.abs(dx)<0.1)disObj.x=recoverX;
if(Math.abs(dy)<0.1)disObj.y=recoverY;
if(dx==0&&dy==0)
{
trace("shake complete");
var tdata:Object=SC_SHAKE_DATA_DIC[disObj];
delete SC_SHAKE_DATA_DIC[disObj];
tdata=null;
disObj.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
data.vx=vx;
data.vy=vy;
}
}
}
外部使用该工具类API,非常方便:
下载运行实例:ShakeAPITest.rar
package
{
import controlsEvents.DirectionPickerEvent;
import controlsEvents.NumericeUpDownEvent;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.net.getClassByAlias;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
import flash.utils.getTimer;
public class ShakeAPITest extends Sprite
{
private var _sp:Sprite;
private var _frictionX:Number=.3;
private var _frictionY:Number=.3;
private var _springX:Number=3;
private var _springY:Number=3;
private var _earthMass:Number=60;//
private var _hitterMass:Number=100;
private var _vx:Number=0;
private var _vy:Number=0;
private var _spSourceX:Number=0;
private var _spSourceY:Number=0;
private var _mc:MovieClip;
private var _amplitude:Number=100;//振幅
private var _duration:Number=100;//单位:毫秒
private var _startShakeTime:Number=0;//开始振动的计时
private var _shakeDirection:Number=0;//振动方向,单位:度(0~360)
private var _shakeOffsetX:Number=2;//X方向的振动抖动偏移量
private var _shakeOffsetY:Number=2;//Y方向的振动抖动偏移量
private var _directionNUD:NumericUpDown;
[SWF(width="1200",height="720")]
public function ShakeAPITest()
{
super();
stage.frameRate=60;
stage.color=0xccccaa;
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
_sp=new Sprite();
addChild(_sp);
// _sp.graphics.beginFill(0xff0000);
// _sp.graphics.drawCircle(0,0,20);
// _sp.graphics.endFill();
//
// _spSourceX=_sp.x=200;
// _spSourceY=_sp.y=100;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownHandler);
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onPicLoadedHandler);
loader.load(new URLRequest("res/test1.jpg"),new LoaderContext(false, ApplicationDomain.currentDomain));
var loader3:Loader=new Loader();
loader3.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwf3LoadedHandler);
loader3.load(new URLRequest("res/vDropball.swf"),new LoaderContext(false, ApplicationDomain.currentDomain));
var durationLable:Label=new Label();
addChild(durationLable);
durationLable.fontSize=15;
durationLable.text="振动持续时间:(100~5000)毫秒";
durationLable.x=100;
durationLable.y=100;
var durationNumericeUpDown:NumericUpDown=new NumericUpDown();
addChild(durationNumericeUpDown);
durationNumericeUpDown.fontColor=0;
durationNumericeUpDown.maxValue=5000;
durationNumericeUpDown.minValue=100;
durationNumericeUpDown.x=durationLable.x+durationLable.width;
durationNumericeUpDown.y=durationLable.y;
durationNumericeUpDown.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,ondurationNumericeUpDownValueChange);
durationNumericeUpDown.curValue=100;
var shakAmplitudeLable:Label=new Label();
addChild(shakAmplitudeLable);
shakAmplitudeLable.fontSize=15;
shakAmplitudeLable.text="振幅:(100~2000)";
shakAmplitudeLable.x=100;
shakAmplitudeLable.y=120;
var shakAmplitudeNumericeUpDown:NumericUpDown=new NumericUpDown();
addChild(shakAmplitudeNumericeUpDown);
shakAmplitudeNumericeUpDown.fontColor=0;
shakAmplitudeNumericeUpDown.minValue=100;
shakAmplitudeNumericeUpDown.maxValue=2000;
shakAmplitudeNumericeUpDown.x=shakAmplitudeLable.x+shakAmplitudeLable.width;
shakAmplitudeNumericeUpDown.y=shakAmplitudeLable.y;
shakAmplitudeNumericeUpDown.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onshakAmplitudeNumericeUpDownValueChange);
shakAmplitudeNumericeUpDown.curValue=100;
var scaleXYLable:Label=new Label();
addChild(scaleXYLable);
scaleXYLable.fontSize=15;
scaleXYLable.text="振动对象缩放比率:(0.1~1)";
scaleXYLable.x=100;
scaleXYLable.y=140;
var scaleXYNUD:NumericUpDown=new NumericUpDown();
addChild(scaleXYNUD);
scaleXYNUD.fontColor=0;
scaleXYNUD.minValue=0.1;
scaleXYNUD.maxValue=1;
scaleXYNUD.increment=0.01;
scaleXYNUD.x=scaleXYLable.x+scaleXYLable.width;
scaleXYNUD.y=scaleXYLable.y;
scaleXYNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onscaleXYNUDNumericeUpDownValueChange);
scaleXYNUD.curValue=100;
var sourceXLable:Label=new Label();
addChild(sourceXLable);
sourceXLable.fontSize=15;
sourceXLable.text="振动对象x:(-1200~1200)";
sourceXLable.x=100;
sourceXLable.y=160;
var sourceXNUD:NumericUpDown=new NumericUpDown();
addChild(sourceXNUD);
sourceXNUD.fontColor=0;
sourceXNUD.minValue=-1200;
sourceXNUD.maxValue=1200;
sourceXNUD.increment=5;
sourceXNUD.x=sourceXLable.x+sourceXLable.width;
sourceXNUD.y=sourceXLable.y;
sourceXNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onsourceXNUDNumericeUpDownValueChange);
sourceXNUD.curValue=100;
var sourceYLable:Label=new Label();
addChild(sourceYLable);
sourceYLable.fontSize=15;
sourceYLable.text="振动对象y:(-720~720)";
sourceYLable.x=100;
sourceYLable.y=180;
var sourceYNUD:NumericUpDown=new NumericUpDown();
addChild(sourceYNUD);
sourceYNUD.fontColor=0;
sourceYNUD.minValue=-720;
sourceYNUD.maxValue=720;
sourceYNUD.increment=5;
sourceYNUD.x=sourceYLable.x+sourceYLable.width;
sourceYNUD.y=sourceYLable.y;
sourceYNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,onsourceYNUDNumericeUpDownValueChange);
sourceYNUD.curValue=100;
var directionLable:Label=new Label();
addChild(directionLable);
directionLable.fontSize=15;
directionLable.text="振动方向:(0~360)";
directionLable.x=100;
directionLable.y=200;
var directionPicker:DirectionPicker=new DirectionPicker();
addChild(directionPicker);
directionLable.y+=directionPicker.radius;
directionPicker.x=directionLable.x+directionLable.width+directionPicker.radius+10;
directionPicker.y=directionLable.y+directionPicker.radius/2;
directionPicker.addEventListener(DirectionPickerEvent.DEGREE_CHANGED,ondirectionPickerDegreeChanged);
_directionNUD=new NumericUpDown();
addChild(_directionNUD);
_directionNUD.fontColor=0;
_directionNUD.minValue=0;
_directionNUD.maxValue=360;
_directionNUD.x=directionPicker.x+directionPicker.radius+10;
_directionNUD.y=directionPicker.y-_directionNUD.height/2;
_directionNUD.addEventListener(NumericeUpDownEvent.VALUE_CHANGED,ondirectionNUDValueChanged);
directionPicker.degree=_directionNUD.curValue=1;
directionPicker.degree=_directionNUD.curValue=0;
var directionLb:Label=new Label("度");
addChild(directionLb);
directionLb.fontSize=15;
directionLb.x=_directionNUD.x+_directionNUD.width;
directionLb.y=_directionNUD.y;
}
private function ondirectionNUDValueChanged(e:NumericeUpDownEvent):void
{
_shakeDirection=e.value;
}
private function ondirectionPickerDegreeChanged(e:DirectionPickerEvent):void
{
_directionNUD.curValue=e.degree;
}
private function onsourceYNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_spSourceY=_sp.y=e.value;
}
private function onsourceXNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_spSourceX=_sp.x=e.value;
}
private function onscaleXYNUDNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_sp.scaleX=_sp.scaleY=e.value;
}
private function onshakAmplitudeNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_amplitude=e.value;
}
private function ondurationNumericeUpDownValueChange(e:NumericeUpDownEvent):void
{
_duration=e.value;
}
private function onSwf3LoadedHandler(e:Event):void
{
_mc=e.currentTarget.content as MovieClip;
}
private function onPicLoadedHandler(e:Event):void
{
var bmp:Bitmap=e.currentTarget.content as Bitmap;
_sp.addChild(bmp);
_sp.setChildIndex(bmp,0);
}
private function onKeyDownHandler(e:KeyboardEvent):void
{
// trace(e.keyCode);//32 == sapce key
if(e.keyCode==32)
{
//shake
// 弹性公式:
// vx += (targetX - sprite.x) * spring;//spring为弹性系数
// vy += (targetY - sprite.y) * spring;
// sprite.x += (vx *= friction);//friction为摩擦力
// sprite.y += (vy *= friction);
var c:Class=ApplicationDomain.currentDomain.getDefinition("vDropBall") as Class;
var mc:MovieClip=new c() as MovieClip;
mc.x=Math.random()*300+100;mc.y=Math.random()*400+100;
mc.addFrameScript(mc.totalFrames-1,
function():void
{
mc.addFrameScript(mc.totalFrames-1,null);
_sp.removeChild(mc);
mc=null;
//这里调用振动API
Utils.shake(_sp,_duration,_amplitude,Utils.SpecialDirecitonShake,_shakeDirection,_spSourceX,_spSourceY,_shakeOffsetX,_shakeOffsetY);
});
_sp.addChild(mc);
_sp.setChildIndex(mc,_sp.numChildren-1);
}
}
}
}