1 //获取类型 2 var my_item=require("Component");//AA:其他类名 1 3 cc.Class({ 4 //cc.Class 返回了一个构造函数,然后继承了cc.Component 5 //cc.Component ,也叫周期函数,也叫组件(固定的入口函数)入口函数 6 extends: cc.Component, 7 8 properties: { 9 //基本数据类型 数 bool 字符串 color pos size 10 speed:110, 11 is_debug:false, 12 url:"", 13 color:cc.color(0,0,0,255), 14 pos:cc.v2(0,1), 15 pos2:cc.p(0,0), 16 size:cc.p(100,100), 17 //系统组件 18 spriteItem:{ 19 type:cc.Sprite, 20 default:null,// 若为单个sprite则为null,否则为数组[] 21 }, 22 spriteItemList:{ 23 type:cc.Sprite, 24 default:[], 25 }, 26 //定义自己的组件 1 27 //用户组件 28 custom_com:{ 29 type:my_item, 30 default:null, 31 }, 32 }, 33 /**组件入口函数 34 * 1: onLoad: 组件加载的时候调用, 保证了你可以获取到场景中的其他节点,以及节点关联的资源数据 35 2: start: 也就是第一次执行 update 之前触发 36 3: update(dt):组件每次刷新的时候调用,dt距离上一次刷新的时间(会在所有画面更新前执行) 37 4: lateUpdate(dt) 刷新完后调用(会在所有画面更新后执行); 38 5: onEnable: 启用这个组件的时候调用; 39 6: onDisable: 禁用这个组件的时候调用; 40 7: onDestroy: 组件实例销毁的时候调用; * 41 */ 42 // start () { }, 43 //update (dt) {}, 44 //lateUpdate(dt){}, 45 // onEnable(){}, 46 // onDestroy(){}, 47 /**cc.Component 48 * 1: 组件类: 所有组件的基类; 49 2: node: 指向这个组件实例所挂载的这个节点(cc.Node); 50 3: name: 这个组件实例所挂载的节点的名字<组件的名字>; 51 4: properties: { 52 } 属性列表; 53 (1) name: value, 数,bool, 字符串; 54 (2) 位置,颜色, 大小: cc.p(0, 0), cc.color(0, 0), cc.size(100, 100) 55 (3) 组件: { 56 type: 组件类型, 系统类型,也可以require自己编写的组件类型 57 default: null or [] 58 } 59 (4)其他: 安装目录内engine,打开cocos creator源码,找到参考,然后移动到你的代码里面; 60 //注:在和服务端交互的返回函数内,尽量不要使用 this等,this在返回函数内代表的应该是返回信息resp 61 */ 62 onLoad0:function () { 63 //const self=this; 64 cc.log(this.node); 65 cc.log(this.name);//组件实例所挂载的节点的名称<组件名称> 66 cc.log(this.node.name);//当前节点名字; 67 }, 68 /**组件添加 查找 删除 69 * 1: addComponent(组件的类型): 向节点上添加一个组件实例, 返回添加好的组件实例;利用this/ this.node 70 2: getComponent(组件类型): 查找一个为指定类型的组件实例(如果有多个,第一个匹配); 71 3: getComponents(组件类型): 查找这个节点上所有这个类型的组件实例; 72 [inst1, inst2, inst3, ...] 73 4: getComponentInChildren(组件类型): 在自己与孩子节点里面查找; 74 5: getComponentsInChildren (组件类型): 在自己与孩子节点里面查找; 75 6: destroy(): 从节点中删除这个组件的实例; 76 */ 77 start0:function(){ 78 var com_=this.addComponent("my_item");//返回挂载的节点实例 79 var com_1=this.node.addComponent("my_item"); 80 //get 81 //返回找到组建的第一个 82 com_1=this.getComponent("my_item"); 83 //返回的是组件数组(实例1 实例2 ...) 84 var com_list=this.getComponent("my_item"); 85 //删除组件,删除当前组件实例,触发onDiaable,onDestroy的调用 86 this.destroy(); 87 }, 88 /**Shedule定时器操作 89 * 1:sheduleOnce(函数, time): time秒后启动一次定时器; 90 2: schedule(函数, time, 次数, 多长时间后开始); 执行的次数为(次数 + 1), cc.macro.REPEAT_FOREVER 91 3: unschedule(函数); // 取消这个定时器操作; 92 5: unscheduleAllCallbacks 取消所有的定时器操作; 93 注意,如果节点或组件没有激活是不会调用的; 94 节点或组件,必须是激活状态 95 */ 96 onLoad:function(){ 97 this.scheduleOnce(function(){ 98 console.log("3s后调用"); 99 }.bind(this),3); 100 this.schedule(function(){ 101 console.log("called"); 102 }.bind(this),1,4,2);//2s后调用,每隔1s再次调用,共调用4+1次; 103 //一直执行 104 this.schedule(function(){ 105 console.log("called"); 106 //this.unscheduleAllCallbacks(); 107 }.bind(this),1,cc.macro.REPEAT_FOREVER ,2); 108 //只取消一个定时器 109 var call=function(){ 110 console.log("aaaa"); 111 }.bind(this); 112 //默认立刻执行,一直循环 113 this.schedule(call,0.5); 114 115 this.scheduleOnce(function(){ 116 this.unschedule(call);//取消call定时器 117 }.bind(this),5); 118 119 }, 120 });
Cocos Creator 系统学习笔记(七)cc.Component的使用详解
猜你喜欢
转载自www.cnblogs.com/allyh/p/9975449.html
今日推荐
周排行