逐帧动画组件源码分享

/**
 * 逐帧动画组件
 */
(function() {

    var Animation = function(cfg) {

        /**
         * 动画图片
         */
        this.image = null;
        /**
         * 帧列表
         * @format {
         *     x: 0,
         *     y: 0,
         *     duration: 0,
         *     collRect: [[left, top, width, height]]
         * }
         */
        this.frames = [];
        /**
         * 循环播放
         */
        this.loop = true;
        /**
         * 播放倍速
         */
        this.speed = 1;
        /**
         * @read only
         * 播放状态
         */
        this.playing = false;
        /**
         * @read only
         * 正在播放的帧索引(第一帧从0开始)
         */
        this.currentFrameIndex = 0;
        /**
         * @read only
         * 正在播放的帧对象
         */
        this.currentFrame = null;
        /**
         * @private
         * 当前帧已播放时间(ms)
         */
        this.__framePlayedDuration = 0;

        Animation.superclass.constructor.call(this, cfg);
    }
    my.inherit(Animation, my.Component);

    /**
     * 事件定义
     * onplay 开始播放动画
     * onstop 停止播放动画
     * onend 动画播放完毕
     */
    Animation.prototype.onplay = my.fn;
    Animation.prototype.onstop = my.fn;
    Animation.prototype.onend = my.fn;

    /**
     * @private
     * 跳转到指定的帧
     * @param {Number} index
     */
    Animation.prototype.__gotoFrame = function(index) {
        if(this.frames[index]) {
            this.currentFrameIndex = index;
            this.currentFrame = this.frames[index];
            this.__framePlayedDuration = 0;
        }
    }
    /**
     * @private
     * 跳转到下一帧
     */
    Animation.prototype.__nextFrame = function() {
        if(this.currentFrameIndex < this.frames.length - 1) {
            this.__gotoFrame(this.currentFrameIndex + 1);
        } else if(this.loop) {
            this.__gotoFrame(0);
        } else {
            this.stop();
            this.onend();
        }
    }
    /**
     * 初始化
     */
    Animation.prototype.init = function() {
        this.__gotoFrame(0);
        Animation.superclass.init.call(this);
    }
    /**
     * 播放动画
     */
    Animation.prototype.play = function() {
        this.playing = true;
        this.onplay();
    }
    /**
     * 停止播放
     */
    Animation.prototype.stop = function() {
        this.playing = false;
        this.onstop();
    }
    /**
     * 跳转到指定帧并开始播放
     * @param {Number} index
     */
    Animation.prototype.gotoAndPlay = function(index) {
        this.__gotoFrame(index);
        this.play();
    }
    /**
     * 跳转到指定帧并停止播放
     * @param {Number} index
     */
    Animation.prototype.gotoAndStop = function(index) {
        this.__gotoFrame(index);
        this.stop();
    }
    /**
     * 更新动画的状态
     * @param {Number} deltaTime
     */
    Animation.prototype.update = function(deltaTime) {
        if(!this.playing) {
        } else if(this.__framePlayedDuration >= this.currentFrame.duration) {
            this.__nextFrame();
        } else {
            this.__framePlayedDuration += deltaTime * this.speed;
        }
    }
    /**
     * 销毁对象
     */
    Animation.prototype.destory = function() {
        this.image = this.frames = this.currentFrame = this.onplay = this.onstop = this.onend = null;
        Animation.superclass.destory.call(this);
    }

    my.Animation = Animation;
})();

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值