欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flash AS3教程:Motion類

互聯(lián)網(wǎng)   發(fā)布時(shí)間:2008-10-06 01:25:49   作者:佚名   我要評(píng)論
前面教程學(xué)習(xí)了Flash AS3教程:Random類,這篇我們一起來學(xué)習(xí)Motion類的使用方法和實(shí)例。 先來一個(gè)例子展示: http://www.webjx.com/files/media/Motion.swf 這個(gè)類貌似是多余的,反正就是Tween類,但是解決了動(dòng)畫可能播到一半就停止了等問題,Tween播放到一半就停止了原
前面教程學(xué)習(xí)了Flash AS3教程:Random類,這篇我們一起來學(xué)習(xí)Motion類的使用方法和實(shí)例。
先來一個(gè)例子展示:
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);
}
}
}

相關(guān)文章

  • flash cs6鼠標(biāo)跟隨效果實(shí)現(xiàn)代碼分享

    flash cs6想要實(shí)現(xiàn)鼠標(biāo)跟隨效果?該怎么制作呢?今天我們就來看看使用as2.0實(shí)現(xiàn)鼠標(biāo)跟隨效果的教程,需要的朋友可以參考下
    2019-05-19
  • Flash cs6怎么使用代碼輸入中英文文本?

    Flash cs6怎么使用代碼輸入中英文文本?Flash cs6中可以使用文字工具直接輸入文本,也可以使用代碼來輸入文本,該怎么使用代碼輸入文本呢?請(qǐng)看下文詳細(xì)的教程,需要的朋友
    2018-03-11
  • flash as3.0怎么定義抽象類和抽象?

    flash as3.0抽象類怎么定義? as3.0中有很多抽象類,該怎么定義抽象類和抽象方法呢?下面我們就來看看簡(jiǎn)單的例子,需要的朋友可以參考下http://www.dbjr.com.cn/softs/408402.
    2018-02-28
  • flash cs6中怎么使用ActionScript3.0?

    flash cs6中怎么使用ActionScript3.0?flash cs6中想要使用ActionScript3.0功能,該怎么使用呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下
    2018-01-25
  • Flash中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?

    本教程給大家分享一個(gè)Flash小教程,教大家在Flash CS6中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?方法很簡(jiǎn)單,感興趣的朋友歡迎前來一起分享學(xué)習(xí)
    2018-01-12
  • Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置?

    本教程教腳本之家的ActionScript教程學(xué)習(xí)者在Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置,教程講解的詳細(xì),感興趣的朋友歡迎前來分享學(xué)習(xí)
    2017-11-20
  • 在Flash CS6中使用with函數(shù)繪制背景圖教程

    本教程教腳本之家的ActionScript教程學(xué)習(xí)者如何在Flash CS6中使用with函數(shù)繪制背景圖?教程一步步講解的挺詳細(xì),方法也不難,非常適合Flash新手入門學(xué)習(xí)
    2017-11-18
  • Flash怎么設(shè)置元件坐標(biāo)?flash使用代碼設(shè)置元件的坐標(biāo)的教程

    Flash怎么設(shè)置元件坐標(biāo)?flash中導(dǎo)如的元件需要添加坐標(biāo),該怎么定位元件坐標(biāo)呢?下面我們就來看看flash使用代碼設(shè)置元件的坐標(biāo)的教程,需要的朋友可以參考下
    2017-10-11
  • Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?

    Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?Flash中想要給花朵制作一段搖擺的動(dòng)畫效果,該怎么制作呢?下面我們就來看看詳細(xì)的教程,很簡(jiǎn)單,需要的朋友可以參考下
    2017-05-23
  • Flash怎么制作流動(dòng)七彩色的文字?

    Flash怎么制作流動(dòng)七彩色的文字?想要讓文字動(dòng)起來,該怎么使用flash給文字制作一個(gè)流動(dòng)七彩色的動(dòng)畫呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下
    2017-04-23

最新評(píng)論