http://www.dbjr.com.cn/files/media/Motion.swf
這個(gè)類貌似是多余的,反正就是Tween類,但是解決了動(dòng)畫可能播到一半就停止了等問題,Tween播放到一半就停止了原因是因?yàn)轭愔械膫陕燛nterFrame事件的時(shí)候,使用的是弱引用偵聽方式,在播放的途中,被內(nèi)存自動(dòng)回收了,因此播放到一半就夭折了,解決辦法嘛,除了自己寫一個(gè),也可以去包中改一下Tween,把偵聽改成強(qiáng)引用就行了
我個(gè)人是不太習(xí)慣使用Tween的,因此我就寫了這個(gè)類,自己使用嘛,大家覺得還不錯(cuò)就拿去用吧
緩動(dòng)的算法還是使用ADOBE自帶的那個(gè)easing包
這個(gè)類屬性和方法比較多。。耐心看吧,跟Tween差不了太多的
類講解:
index.base.animation.Motion類:
代碼:public class Motion extends EventDispatcher
提供給程序員使用的動(dòng)畫類
構(gòu)造函數(shù):
public function Motion(target_:*,_attribute:String,_algorithm:Function,_begin:Number,_end:Number,_duration:uint = 10)
與Tween一模一樣,只不過最后少了個(gè)屬性是是否以時(shí)間計(jì)算緩動(dòng),而該類只有以幀頻計(jì)算緩動(dòng)
play方法:
public function play():void
開始播放,并觸發(fā)播放事件
如果正在播放,調(diào)用該方法不會(huì)有什么變化,只不過會(huì)觸發(fā)播放事件
back方法:
public function back():void
同于play方法,不同的是該方法是讓動(dòng)畫反過來播放
resume方法:
public function resume():void
繼續(xù)播放,依然會(huì)觸發(fā)播放事件
stop方法:
public function stop():void
停止播放,觸發(fā)停止事件
如果是播放完畢了,即還會(huì)觸發(fā)播放完畢事件
停止事件永遠(yuǎn)比播放完畢事件提前調(diào)度
reset方法:
public function reset():void
重置動(dòng)畫,還原到剛開始實(shí)例化的狀態(tài)
無論是否正在播放,都會(huì)觸發(fā)停止事件
forward方法:
public function forward():void
快進(jìn)到最后
rewind方法:
public function rewind():void
倒帶到最前
next方法:
public function next():void
向前播放一幀
如果是在播放中使用該方法,效果不是太明顯
prev方法:
public function prev():void
向前播放一幀
如果是在播放中使用該方法,效果不是太明顯
clear方法:
public function clear():void
清除類中的引用,事件等
isBack屬性(只讀):
public function get isBack():Boolean
是否在回放狀態(tài)
target屬性(只讀):
public function get target():*
獲取當(dāng)前操作的對(duì)象
current屬性(只讀):
public function get current():uint
獲取當(dāng)前播放的位置
playing屬性(只讀):
public function get playing():Boolean
是否正在播放
attribute屬性:
public var attribute:String;
設(shè)置操作的對(duì)象屬性,沒必要的情況下最好不要修改
begin屬性:
public var begin:Number;
設(shè)置操作的對(duì)象初始屬性,沒必要的情況下最好不要修改
end屬性:
public var end:Number;
設(shè)置操作的對(duì)象結(jié)束屬性,沒必要的情況下最好不要修改
duration屬性:
public var duration:uint;
設(shè)置對(duì)象從初始值,經(jīng)過多少幀,才運(yùn)動(dòng)到結(jié)束值
algorithm屬性:
public var algorithm:Function;
設(shè)置對(duì)象從初始值到結(jié)束值是以什么算法進(jìn)行運(yùn)動(dòng)
受保護(hù)的屬性:
protected var _current:uint = 0;
protected function updata(isInit:Boolean = false):void
如果繼承該類,則可以訪問_current屬性和updata方法,可以直接修改當(dāng)前幀和強(qiáng)制更新屏幕
舉例:(上面那個(gè)展示flash的源代碼)
對(duì)于各種不同的算法,進(jìn)行效果展示,小小的偷了一下懶,使用的flash自帶組件。。
CODE:
import fl.transitions.easing.*;
import index.base.animation.Motion;
import index.base.events.MotionEvent;
//算法數(shù)組
var classAr:Array = [Back,Bounce,Elastic,None,Regular,Strong];
//初始小方塊
var mc:MC = new MC;
mc.y = 150;
addChild(mc);
//動(dòng)畫聲明
var motion:Motion = new Motion(mc,"x",Back.easeIn,50,350,40);
motion.addEventListener(MotionEvent.MOTION_UPDATA,motionUpdataFun);
motion.addEventListener(MotionEvent.MOTION_STOP,motionStopFun);
motion.addEventListener(MotionEvent.MOTION_PLAY,motionPlayFun);
motion.addEventListener(MotionEvent.MOTION_FINISH,motionFinishFun);
motion.play();
//動(dòng)畫播放完畢
function motionFinishFun(e:MotionEvent){
traceText.appendText("播放完畢\n");
motion.isBack ? motion.play() : motion.back();
traceText.scrollV = traceText.maxScrollV;
}
//屏幕更新
function motionUpdataFun(e:MotionEvent){
currentText.text = motion.current.toString();
traceText.appendText("屏幕更新,當(dāng)前幀 " motion.current ",X屬性:" mc.x "\n");
traceText.scrollV = traceText.maxScrollV;
}
//動(dòng)畫播放
function motionPlayFun(e:MotionEvent){
traceText.appendText("開始播放\n");
traceText.scrollV = traceText.maxScrollV;
}
//動(dòng)畫停止
function motionStopFun(e:MotionEvent){
traceText.appendText("停止播放\n");
traceText.scrollV = traceText.maxScrollV;
}
//偵聽各個(gè)面板的change事件
classList.addEventListener("change",changeFun);
funcList.addEventListener("change",changeFun);
durationBar.addEventListener("change",changeFun);
playButton.addEventListener("click",clickFun);
//當(dāng)屬性面板發(fā)生數(shù)值改變,即觸發(fā)
function changeFun(e:Event){
motion.rewind();
motion.algorithm = classAr[classList.selectedItem.data][funcList.selectedItem.data];
motion.duration = durationBar.value;
durationText.text = durationBar.value.toString();
}
//播放按鈕
function clickFun(e:Event){
if(playButton.selected) motion.resume();
else motion.stop();
}
//4個(gè)倒帶前進(jìn)等按鈕事件
prevButton.addEventListener(MouseEvent.CLICK,function(){motion.prev()});
nextButton.addEventListener(MouseEvent.CLICK,function(){motion.next()});
forwardButton.addEventListener(MouseEvent.CLICK,function(){motion.forward()});
rewindButton.addEventListener(MouseEvent.CLICK,function(){motion.rewind()});
類源代碼:
CODE:
package index.base.animation{
import flash.events.Event;
import flash.events.EventDispatcher;
import index.base.events.MotionEvent;
public class Motion extends EventDispatcher{
//公共屬性
public var attribute:String;
public var begin:Number;
public var end:Number;
public var duration:uint;
public var algorithm:Function;
//受保護(hù)的屬性
protected var _current:uint = 0;
//私有屬性
private var _target:*;
private var _playing:Boolean = false;
private var _append:int = 1;
public function Motion(target_:*,_attribute:String,_algorithm:Function,_begin:Number,_end:Number,_duration:uint = 10){
_target = target_;
attribute = _attribute;
begin = _begin;
end = _end;
duration = _duration;
algorithm = _algorithm;
}
//開始播放
public function play():void{
_append = 1;
resume();
}
//回放
public function back():void{
_append = -1;
resume();
}
//繼續(xù)播放
public function resume():void{
if(_playing) _target.removeEventListener(Event.ENTER_FRAME,enterFrameFun);
_playing = true;
_target.addEventListener(Event.ENTER_FRAME,enterFrameFun);
//觸發(fā)開始播放事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_PLAY));
}
//幀頻執(zhí)行事件
private function enterFrameFun(e:Event):void{
if((_append == 1 && _current >= duration) || (_append == -1 && _current <= 0)){
stop();
}else{
_current = _append;
updata();
}
}
//停止播放
public function stop():void{
_playing = false;
_target.removeEventListener(Event.ENTER_FRAME,enterFrameFun);
//觸發(fā)停止事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_STOP));
//觸發(fā)播放完畢事件
if(_current == duration || _current == 0) dispatchEvent(new MotionEvent(MotionEvent.MOTION_FINISH));
}
//更新動(dòng)畫
protected function updata(isInit:Boolean = false):void{
if(isInit) _current = 0;
_target[attribute] = algorithm(_current,begin,end - begin,duration);
//觸發(fā)屏幕更新事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_UPDATA));
}
//重置
public function reset():void{
stop();
updata(true);
_append = 1;
}
//前進(jìn)到最后
public function forward():void{
_current = duration;
updata();
}
//倒帶到最前
public function rewind():void{
_current = 0;
updata();
}
//快進(jìn)一幀
public function next():void{
if(_current < duration){
_current = 1;
updata();
}
}
//后退一幀
public function prev():void{
if(_current > 0){
_current -= 1;
updata();
}
}
//清除
public function clear():void{
_target.removeEventListener(Event.ENTER_FRAME,enterFrameFun);
_target = null;
algorithm = null;
}
//獲取是否為回放狀態(tài)
public function get isBack():Boolean{
return Boolean(_append - 1);
}
//獲取緩動(dòng)目標(biāo)
public function get target():*{
return _target;
}
//獲取當(dāng)前運(yùn)行幀數(shù)
public function get current():uint{
return _current;
}
//獲取當(dāng)前是否在運(yùn)行
public function get playing():Boolean{
return _playing;
}
}
}
事件類源代碼:
CODE:
package index.base.events{
import flash.events.Event;
public class MotionEvent extends Event{
public static const MOTION_STOP:String = "motionStop";//播放停止
public static const MOTION_FINISH:String = "motionFinish";//播放完畢
public static const MOTION_PLAY:String = "motionPlay";//開始播放
public static const MOTION_UPDATA:String = "motionUpdata";//畫面更新
public function MotionEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false){
super(type,bubbles,cancelable);
}
}
}