版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/themagickeyjianan/article/details/86190324
/**
* 皮肤动画数组
*/
var cow_skin = cc.Class({
name: "cow_skin",
properties: {
cow_anim: {
type: cc.SpriteFrame,
default:[],
},
}
});
//
cc.Class({
extends: cc.Component,
properties: {
cow_skin_set: {
default:[],
type: cow_skin,
},
},
// LIFE-CYCLE CALLBACKS:
onLoad: function() {
// game_scene组件
this.game_scene = cc.find("Canvas").getComponent("game_scene");
// 主动以代码形式为节点添加一个自定义组件
this.anim_com = this.node.addComponent("frame_anim");
this.cow_type = Math.floor(Math.random() * 3 + 1); //1-4 但是只能取到3,做下越界处理
if(this.cow_type >= 4){
this.cow_type = 1;
}
this.speed_x = -(Math.random() * 100 + 100);
this._play_cow_walk();
},
_play_cow_walk: function(){
this.anim_com.sprite_frames = this.cow_skin_set[this.cow_type - 1].cow_anim;
this.anim_com.duration = 0.2;
this.anim_com.play_loop();
},
start () {
},
update (dt) {
var sx = this.speed_x * dt;
this.node.x += sx;
},
});
1)一次只有一个场景,所以可以从Canvas下面索引当前场景上的组件
this.game_scene = cc.find("Canvas").getComponent("game_scene");
2)可以动态给一个节点通过代码的形式给节点增加一个组件,而不用再编辑器中挂载的方式
this.anim_com = this.node.addComponent("frame_anim");