```
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);
}
}
网友评论