帧动画是游戏中特效表现的必修课。那么我们使用代码要怎么样来实现帧动画的效果呢?
第一步、定义帧动画的一些属性:
定义一些属性,方便编辑器上调试效果:帧动画的图片数组、2帧之间的时间间隔、是否循环、是否加载时播放等;
properties: {
//帧动画的图片数组
sprite_frames: {
type: cc.SpriteFrame,
default: [],
},
//时间间隔
duration: 0.1,
//是否循环
is_loop: false,
//是否加载时播放
play_onload: false,
},
第二步、两个函数:循环播放、一次播放
//循环播放
play_loop: function(){
if(this.sprite_frames.length <= 0){
return;
}
this.is_loop = true;
this.end_func = null;
this.is_playing = true;
this.sprite.spriteFrame = this.sprite_frames[0];
},
//一次播放
play_once: function(end_func){
if(this.sprite_frames.length <= 0){
return;
}
this.end_f