在节点上加上这个脚本,然后将每阵的spriteFrame拖入即可
cc.Class({
extends: cc.Component,
properties: {
frames:{
type: cc.SpriteFrame,
default:[]
},
duration: 0.1,
isloop: false,
play_onload: false,
},
onLoad () {
this.sprite = this.getComponent(cc.Sprite);
if(!this.sprite){
this.addComponent(cc.Sprite);
}
},
start () {
this.no_frame = false;
if (this.frames.length <= 0) {
this.no_frame = true;
return;
}
this.sprite.spriteFrame = this.frames[0];
this.playing = false;
this.end_func = null;
this.total_time = 0;
if(this.play_onload)
{
if (this.isloop !== true){
this.play_once();
}
else{
this.play_loop();
}
}
},
play_once(end_func){
this.playing = true;
this.end_func = end_func;
},
play_loop(){
this.isloop = true;
this.playing = true;
},
stop_anim(){
this.isloop = false;
this.playing = false;
this.total_time = 0;
},
update (dt) { // 改变每一帧的显示
if (this.no_frame === true || this.playing !== true){
return;
}
this.total_time += dt;
var index = Math.floor(this.total_time / this.duration);
if (this.isloop !== true){
if (index >= this.frames.length){ // 播放完了
this.sprite.spriteFrame = this.frames[index - 1];
this.total_time = 0;
this.playing = false;
return;
}
this.sprite.spriteFrame = this.frames[index];
}
else{
if (index >= this.frames.length){ // 播放完了
index = index - 1;
this.total_time = 0;
}
this.sprite.spriteFrame = this.frames[index];
}
},
});