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(valuemax)value=max;
return value;
}
private function onEnterFrameHandler(e:Event):void
{
trace("get vx,vy",_vx,_vy);
if((getTimer()-_startShakeTime)
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?