导航

  • 首页
  • Tags列表
  • 管理
  • 简繁转换
Search Engine Optimization 站外搜索 站内搜索
« google整合了Gtalk与OrkutTween类 »

Motion类

又有一段时间不玩flash了,AS2.0的语法规则又忘了差不多了,囫囵吞枣式的又看了一遍,反正AS3.0都出来了。花了两天时间改了《flash MX编程与创意实现》这本书中的Tween类。在修改过程中遇到了一些困难,"偷窥"了一下flash8中自带的TWeen类,终于问题得已解决。这里是Tween类的父类:Motion类(flash8中的Tween类没有父类),Tween类与实例文件稍后奉上,源码如下:

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();
  }
}

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Search



  • 互联网 站内搜索

最新留言

最近发表

站点统计

  • 文章总数:244
  • 评论总数:246
  • 引用总数:0
  • 浏览总数:380993
  • 当前样式:default
  • 当前语言:zh-CN

图标汇集

  • 订阅到抓虾
    新闻蚂蚁
    周博通
    订阅到狗狗
    google reader
    bloglines
    my yahoo
    newsgator
    netvibes
    Rojo
  • 通过 W3C XHTML 1.0 Transitional 校验
  • 通过 W3C CSS 校验
  • 订阅本站的 ATOM 1.0 新闻聚合
  • 订阅本站的 RSS 2.0 新闻聚合

Powered By Z-Blog 1.6 Final Build 60802

Copyright Yufuzi Some Rights Reserved.
E-mail:yufuzi80@126.com QQ:20168130
备案序号:苏ICP备06015492号