Flash AS3教程:Motion类

互联网   发布时间:2008-10-06 01:25:49   作者:佚名   我要评论
前面教程学习了Flash AS3教程:Random类,这篇我们一起来学习Motion类的使用方法和实例。 先来一个例子展示: http://www.webjx.com/files/media/Motion.swf 这个类貌似是多余的,反正就是Tween类,但是解决了动画可能播到一半就停止了等问题,Tween播放到一半就停止了原

类源代码:

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;

//受保护的属性
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();
}

//继续播放
public function resume():void{
if(_playing) _target.removeEventListener(Event.ENTER_FRAME,enterFrameFun);
_playing = true;
_target.addEventListener(Event.ENTER_FRAME,enterFrameFun);

//触发开始播放事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_PLAY));
}

//帧频执行事件
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);

//触发停止事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_STOP));
//触发播放完毕事件
if(_current == duration || _current == 0) dispatchEvent(new MotionEvent(MotionEvent.MOTION_FINISH));
}

//更新动画
protected function updata(isInit:Boolean = false):void{
if(isInit) _current = 0;
_target[attribute] = algorithm(_current,begin,end - begin,duration);

//触发屏幕更新事件
dispatchEvent(new MotionEvent(MotionEvent.MOTION_UPDATA));
}

//重置
public function reset():void{
stop();
updata(true);
_append = 1;
}

//前进到最后
public function forward():void{
_current = duration;
updata();
}

//倒带到最前
public function rewind():void{
_current = 0;
updata();
}

//快进一帧
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;
}

//获取是否为回放状态
public function get isBack():Boolean{
return Boolean(_append - 1);
}

//获取缓动目标
public function get target():*{
return _target;
}

//获取当前运行帧数
public function get current():uint{
return _current;
}

//获取当前是否在运行
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);
}
}
}

相关文章

  • flash cs6鼠标跟随效果实现代码分享

    flash cs6想要实现鼠标跟随效果?该怎么制作呢?今天我们就来看看使用as2.0实现鼠标跟随效果的教程,需要的朋友可以参考下
    2019-05-19
  • Flash cs6怎么使用代码输入中英文文本?

    Flash cs6怎么使用代码输入中英文文本?Flash cs6中可以使用文字工具直接输入文本,也可以使用代码来输入文本,该怎么使用代码输入文本呢?请看下文详细的教程,需要的朋友
    2018-03-11
  • flash as3.0怎么定义抽象类和抽象?

    flash as3.0抽象类怎么定义? as3.0中有很多抽象类,该怎么定义抽象类和抽象方法呢?下面我们就来看看简单的例子,需要的朋友可以参考下http://www.jb51.net/softs/408402.
    2018-02-28
  • flash cs6中怎么使用ActionScript3.0?

    flash cs6中怎么使用ActionScript3.0?flash cs6中想要使用ActionScript3.0功能,该怎么使用呢?下面我们就来看看详细的教程,需要的朋友可以参考下
    2018-01-25
  • Flash中怎么实现鼠标点击决定图像位置?

    本教程给大家分享一个Flash小教程,教大家在Flash CS6中怎么实现鼠标点击决定图像位置?方法很简单,感兴趣的朋友欢迎前来一起分享学习
    2018-01-12
  • Flash中如何用代码将图片放在自己想要的舞台位置?

    本教程教脚本之家的ActionScript教程学习者在Flash中如何用代码将图片放在自己想要的舞台位置,教程讲解的详细,感兴趣的朋友欢迎前来分享学习
    2017-11-20
  • 在Flash CS6中使用with函数绘制背景图教程

    本教程教脚本之家的ActionScript教程学习者如何在Flash CS6中使用with函数绘制背景图?教程一步步讲解的挺详细,方法也不难,非常适合Flash新手入门学习
    2017-11-18
  • Flash怎么设置元件坐标?flash使用代码设置元件的坐标的教程

    Flash怎么设置元件坐标?flash中导如的元件需要添加坐标,该怎么定位元件坐标呢?下面我们就来看看flash使用代码设置元件的坐标的教程,需要的朋友可以参考下
    2017-10-11
  • Flash怎么制作来回摇摆的花朵的动画?

    Flash怎么制作来回摇摆的花朵的动画?Flash中想要给花朵制作一段摇摆的动画效果,该怎么制作呢?下面我们就来看看详细的教程,很简单,需要的朋友可以参考下
    2017-05-23
  • Flash怎么制作流动七彩色的文字?

    Flash怎么制作流动七彩色的文字?想要让文字动起来,该怎么使用flash给文字制作一个流动七彩色的动画呢?下面我们就来看看详细的教程,需要的朋友可以参考下
    2017-04-23

最新评论