Motion.as
import mx.transitions.OnEnterFrameBeacon;
class Motion {
static var __initBeacon = OnEnterFrameBeacon.init();
/*含有受控属性的对象*/
var obj:MovieClip;
/*受控属性*/
var prop:String;
/*运动起始位置*/
var begin:Number;
/*运动持续时间,以秒或帧为单位。如果没有定义这个参数或参数为0,表示持续时间无限长*/
var duration:Number;
/*是否以秒为计时单位*/
var useSeconds:Boolean;
/*受控对象在整个swf中开始播放的时间*/
var startTime:Number;
/*受控对象播放的当前时间*/
var time:Number;
/*变化之前的位置*/
var prevPos:Number;
/*当前位置*/
var pos:Number;
/*是否循环播放*/
var looping:Boolean;
/*******************************************************/
public var onMotionFinished:Function;
public var onMotionLooped:Function;
public var onMotionChanged:Function;
public var onMotionStarted:Function;
public var onMotionStopped:Function;
public var onMotionResumed:Function;
/*******************************************************/
/*自监听*/
var addListener:Function;
var removeListener:Function;
var broadcastMessage:Function;
private var _listeners:Array;
/*******************************************************/
/*修正starttime*/
private function fixTime():Void {
if (this.useSeconds) {
this.startTime = getTimer()-this.time*1000;
}
}
/*使Motion返回到起始时间*/
function rewind(t:Number):Void {
this.time = (t == undefined) ? 0 : t;
this.fixTime();
}
/*使Motion从当前时间继续播放*/
function resume():Void {
this.fixTime();
MovieClip.addListener(this);
this.broadcastMessage("onMotionResumed", this);
}
/*返回指定时间Motion位置,这个空方法将在子类中被重载*/
function getPosition(t:Number):Number {
return;
}
/*改变Motion位置*/
function setPosition(p:Number):Void {
this.prevPos = this.pos;
this.obj[this.prop] = this.pos=p;
this.broadcastMessage("onMotionChanged", this, this.pos);
}
/*刷新Motion*/
private function update():Void {
this.setPosition(this.getPosition(this.time));
}
function stop():Void {
MovieClip.removeListener(this);
this.broadcastMessage("onMotionStopped", this);
}
/*设定Motion的当前时间*/
function setTime(t:Number):Void {
if (t>this.duration) {
if (this.looping) {
this.rewind(t-this.duration);
this.update();
this.broadcastMessage("onMotionLooped", this);
} else {
if (this.useSeconds) {
this.time = this.duration;
this.update();
}
this.stop();
this.broadcastMessage("onMotionFinished", this);
}
} else if (t<0) {
this.rewind();
this.update();
} else {
this.time = t;
this.update();
}
}
/*使Motion从起始位置开始运动*/
function start():Void {
this.rewind();
MovieClip.addListener(this);
this.broadcastMessage("onMotionStarted", this);
}
/*使Motion快速前进到终点*/
function fforward():Void {
this.setTime(this.duration);
this.fixTime();
}
/*使Motion前进一帧*/
function nextFrame():Void {
if (this.useSeconds) {
this.setTime((getTimer()-this.startTime)/1000);
} else {
this.setTime(this.time+1);
}
}
/*使Motion后退一帧*/
function prevFrame():Void {
if (!this.useSeconds) {
this.setTime(this.time-1);
}
}
function onEnterFrame():Void {
this.nextFrame();
}
function setBegin(b:Number):Void {
this.begin = b;
}
function getBegin():Number {
return this.begin;
}
function setDuration(d:Number):Void {
this.duration = (d == null || d<=0) ? Infinity : d;
}
function getDuration():Number {
return this.duration;
}
function getTime():Number {
return this.time;
}
function setLooping(b:Boolean):Void {
this.looping = b;
}
function getLooping():Boolean {
return this.looping;
}
function setObj(o:MovieClip):Void {
this.obj = o;
}
function getObj():MovieClip {
return this.obj;
}
function setProp(p:String):Void {
this.prop = p;
}
function getProp():String {
return this.prop;
}
function setUseSeconds(useSecs:Boolean):Void {
this.useSeconds = useSecs;
}
function getUseSeconds():Boolean {
return this.useSeconds;
}
function getPrevPos():Number {
return this.prevPos;
}
function toString():String {
return "[Motion prop="+this.prop+" t="+this.time+" pos="+this.getPosition()+"]";
}
function Motion(obj:MovieClip, prop:String, begin:Number, duration:Number, useSeconds:Boolean) {
this.setObj(obj);
this.setProp(prop);
this.setBegin(begin);
this.setPosition(begin);
this.setDuration(duration);
this.setUseSeconds(useSeconds);
AsBroadcaster.initialize(this);
this._listeners=[];
this.addListener(this);
this.start();
}
}









