美文网首页
cocos gif图集播放

cocos gif图集播放

作者: 赤焰军少帅林殊 | 来源:发表于2018-11-09 17:04 被阅读52次
    ```
    

    const {ccclass, property} = cc._decorator;

    @ccclass
    export default class NewClass extends cc.Component {

    @property(cc.Sprite)
    animSpriteFrame : cc.Sprite = null;
    
    @property(cc.SpriteAtlas)
    animAtlas : cc.SpriteAtlas = null;
    
    @property
    speed : number = 0.1;
    
    @property
    delay : number = 0;
    
    @property
    stopAtEmptyFrame : boolean = false;
    
    @property
    isLoop : boolean = true;
    
    @property
    playOnEnable : boolean = true;
    
    @property
    _index : number = 0;
    
    @property([cc.SpriteFrame])
    _spriteFrames : cc.SpriteFrame[] = [];
    
    onEnable(){
        this._spriteFrames = this.animAtlas.getSpriteFrames();
        let atlasName = this.animAtlas.name.split(".")[0];
    
        this._spriteFrames.sort( (a,b) => 
            this._getIndex(a, atlasName) - this._getIndex(b, atlasName)
        );
        
        //console.log(this._spriteFrames);
        if(this.playOnEnable) this.playAnim();
    }
    
    onDisable(){
        this.stopAnim();
    }
    
    _getIndex(sprite : cc.SpriteFrame, atlasName : string) {
        return Number(sprite.name.substring(atlasName.length));
    }
    
    stopAnim(){
        this.unscheduleAllCallbacks();
    }
    
    playAnim(){
        this.scheduleOnce( ()=>{
            this.schedule(() => {
                this.animSpriteFrame.spriteFrame = this._spriteFrames[this._index];
                this._index++;
                if(this._index === this._spriteFrames.length - 1){
                    this._index = 0;
                    if(this.isLoop === false){
                        if(this.stopAtEmptyFrame) this.animSpriteFrame.spriteFrame = null;
                        this.stopAnim();
                    }
                    
                }
            }, this.speed);
        }, this.delay);
    }
    

    }

    相关文章

      网友评论

          本文标题:cocos gif图集播放

          本文链接:https://www.haomeiwen.com/subject/oevoxqtx.html