本文主要是介绍CocosCreator 帧动画 组件脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在节点上加上这个脚本,然后将每阵的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]; }},
});
这篇关于CocosCreator 帧动画 组件脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!