7f6905ec-0e41-4c5d-8827-110a1c8192d3.js 4.83 KB
"use strict";
cc._RF.push(module, '7f690XsDkFMXYgnEQocgZLT', 'FrameAnimation');
// script/common/components/FrameAnimation.ts

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const { ccclass, menu, property } = cc._decorator;
let FrameAnimation = /** @class */ (() => {
    let FrameAnimation = 
    /**序列侦动画组件,用于相同时间间隔序列侦动画 */
    class FrameAnimation extends cc.Component {
        constructor() {
            super(...arguments);
            this.tag = 'animation';
            this.spriteframes = [];
            this.frameTime = 0.1; //帧动画切换图片的时间,即间隔时间
            this.loop = false; //是否循环播放
            this.playOnLoad = false;
        }
        get isPlaying() { return this._isPlaying; }
        get time() { return this._time; }
        onLoad() {
            this._isPlaying = false; //是否在播放
            this._time = 0; //播放时间控制
            this._sprite = this.getComponent(cc.Sprite); //获取组件
            if (!this._sprite) { //若组件为空则为其添加组件
                this._sprite = this.addComponent(cc.Sprite);
            }
            if (this.playOnLoad) { //如果用户选择playload属性则在场景开始时播放动画
                this.play();
            }
        }
        play(callback, loop) {
            if (this.spriteframes.length < 0) {
                return;
            }
            if (loop !== undefined)
                this.loop = loop;
            this._endCallback = callback;
            this._loopCount = 0;
            this._isPlaying = true;
            this._time = 0;
            this._sprite.spriteFrame = this.spriteframes[0];
        }
        stop() {
            this._isPlaying = false;
        }
        update(dt) {
            if (!this._isPlaying) { //判断是否正在播放,不在播放可以直接返回
                return;
            }
            if (this.spriteframes.length < 0) { //判断精灵帧长度是否合法
                return;
            }
            this._time += dt; //获取播放时间
            var index = Math.floor(this._time / this.frameTime); //取整获得图片位置
            if (!this.loop) { //非循环播放
                if (index >= this.spriteframes.length) { //播放完毕
                    this._isPlaying = false;
                    if (this._endCallback) { //执行回调函数
                        this._endCallback();
                    }
                }
                else { //动画正在播放
                    this._sprite.spriteFrame = this.spriteframes[index];
                }
            }
            else { //循环播放
                while (index >= this.spriteframes.length) {
                    //使索引回到之前对应的索引
                    index -= this.spriteframes.length;
                    //数组长度 *间隔时间 = 一段时间段
                    this._time -= (this.spriteframes.length * this.frameTime); //使时间段回到之前对应的时间段
                    this._loopCount++;
                    if (this._endCallback) { //执行回调函数
                        this._endCallback(this._loopCount);
                    }
                }
                this._sprite.spriteFrame = this.spriteframes[index]; //更新索引
            }
        }
    };
    __decorate([
        property({ tooltip: "动画标记" })
    ], FrameAnimation.prototype, "tag", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "精灵帧数组" })
    ], FrameAnimation.prototype, "spriteframes", void 0);
    __decorate([
        property({ type: cc.Float, tooltip: "帧动画切换图片的时间,即间隔时间" })
    ], FrameAnimation.prototype, "frameTime", void 0);
    __decorate([
        property({ tooltip: "是否循环播放" })
    ], FrameAnimation.prototype, "loop", void 0);
    __decorate([
        property({ tooltip: "是否加载完自动播放" })
    ], FrameAnimation.prototype, "playOnLoad", void 0);
    FrameAnimation = __decorate([
        ccclass,
        menu("扩展组件/FrameAnimation")
        /**序列侦动画组件,用于相同时间间隔序列侦动画 */
    ], FrameAnimation);
    return FrameAnimation;
})();
exports.default = FrameAnimation;

cc._RF.pop();