日期:2013-07-05 浏览次数:20586 次
最近做的东西,对于效果编程比较多,特别是缓冲,加速度等效果,特意研讨了一下Tween类,写篇教程分享
给大家,算是过年送给大家的新年礼物!
下面是官方写的fl.transitions.Tween类:
可以先尝试看一下这段代码的写法,用这个方法去看:
从结构函数看起,假设我使用了该类
new Tween(mc,"x",Regular.easeOut,0,200,4);
结合adobe的协助文档,当初始化实例的时候,便对对象mc的x进行Regular.easeOut操作,具体是从0值到200
值,历时4帧
然后问一个为什么,他为什么会对对象mc的x进行Regular.easeOut操作,具体是从0值到200值,历时4帧?
带着这个问题,如今开始看这个类吧!从结构函数看
package fl.transitions
{
import flash.events.*;
import flash.display.*;
import flash.utils.*;
[Event(name="motionChange", type="fl.transitions.TweenEvent")]
[Event(name="motionFinish", type="fl.transitions.TweenEvent")]
[Event(name="motionLoop", type="fl.transitions.TweenEvent")]
[Event(name="motionResume", type="fl.transitions.TweenEvent")]
[Event(name="motionStart", type="fl.transitions.TweenEvent")]
[Event(name="motionStop", type="fl.transitions.TweenEvent")]
public class Tween extends EventDispatcher
{
protected static var _mc:MovieClip = new MovieClip();
public var isPlaying:Boolean = false;
public var obj:Object = null;
public var prop:String = "";
public var func:Function = function (t:Number, b:Number, c:Number, d:Number):Number { return
c*t/d + b; }
public var begin:Number = NaN;
public var change:Number = NaN;
public var useSeconds:Boolean = false;
public var prevTime:Number = NaN;
public var prevPos:Number = NaN;
public var looping:Boolean = false;
private var _duration:Number = NaN;
private var _time:Number = NaN;
private var _fps:Number = NaN;
private var _position:Number = NaN;
private var _startTime:Number = NaN;
private var _intervalID:uint = 0;
private var _finish:Number = NaN;
private var _timer:Timer = null;
public function get time():Number
{
return this._time;
}
public function set time(t:Number):void
{
this.prevTime = this._time;
if (t > this.duration) {
if (this.looping) {
this.rewind (t - this._duration);
this.update();
this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_LOOP, this._time, this._position));
} else {
if (this.useSeconds) {
this._time = this._duration;
this.update();
}
this.stop();
this.dispatchEvent(new TweenEvent(TweenEvent.MOTION_FINISH, this._time,
this._position));
}
} else if (t < 0) {
this.rewind();
this.update();
} else {
this._time = t;
this.update();
}
}
public function get duration():Number
{
return this._duration;
}
public function set duration(d:Number):void
{
this._duration = (d <= 0) ? Infinity : d;
}
public function get FPS():Number
{
return this._fps;
}
&nbs