cocos creator基础-cocos编辑器学习
1 // 拿到当前组件所依附的节点 2 this.node 3 // 拿到当前组件 4 this 5 // 如何取得其他节点 6 7 // 取得节点的父节点 8 this.node.parent 9 // 取得节点的子节点数组 10 this.node.children 11 // 通过节点名字取得子节点 12 this.node.getChildByName('node-3'); 13 // 删除节点 14 this.node5.removeFromParent(); // 这个只是断开与父节点的链接 并没删除 15 this.node5.destroy(); // 真正的删除 16 17 // 实例化一个节点 18 let nodeCopy = cc.instantiate(this.node5Prefab); 19 this.node.addChild(nodeCopy); 20 21 // 动作、动画 22 this.node.runAction( 23 cc.moveTo(1, 20, 20); 24 cc.moveTo(1, 20, 20).easing(cc.easeBackIn(3)); 25 26 cc.rotateTo(1, 180); 27 cc.fadeOut(1); 28 cc.scaleTo(1, 2, 2); 29 30 cc.removeSelf(true || false); 31 ); 32 // 事件 33 // 事件监听 node.on node.once 34 this.node4.on('枪声', this.rush, this.node4); 35 this.node5.on('枪声', this.escape, this.node5); 36 37 rush : function(e){ 38 this // this.node4 39 e.detail // 参数{hello : 'world'} 40 this.runAction(cc.moveTo(1, 200, 0)); 41 } 42 escape : function(){ 43 this // this.node5 44 } 45 46 // 关闭监听 47 node.off 48 49 // 发射事件 50 emit 和 dispatchEvent。 51 两者的区别是,dispatchEvent可以做事件传递(冒泡传送) 52 this.node.emit('枪声', {hello : 'world'}) 53 54 // 触摸事件 55 this.node.on('touchstart', this.onTouchStart, this); 56 this.node.on('touchmove', this.onTouchMove, this); 57 this.node.on('touchend', this.onTouchEnd, this); 58 this.node.on('touchcancel', this.onTouchCancel, this); 59 60 onTouchStart : function(e){ 61 console.log('onTouchStart', e); 62 console.log(e.getLocation()); 63 } 64 onTouchMove : function(e){ 65 console.log('onTouchMove', e); 66 } 67 onTouchEnd : function(e){ 68 console.log('onTouchEnd', e); 69 } 70 onTouchCancel : function(e){ 71 console.log('onTouchCancel', e); 72 } 73 74 // 鼠标事件 75 this.node.on('mouseup', this.mouseup, this); 76 this.node.on('mousemove', this.mousemove, this); 77 this.node.on('mousedown', this.mousedown, this); 78 this.node.on('mouseenter', this.mouseenter, this); 79 this.node.on('mouselevel', this.mouselevel, this); 80 this.node.on('mousewheel', this.mousewheel, this); 81 82 //键盘事件 83 cc.systemEvent.on('keydown', this.onKeyDown, this); 84 onKeyDown : function(e){ 85 if(e.keyCode == cc.KEY.w){ 86 console.log('key w press'); 87 } 88 } 89 cc.systemEvent.on('keyup', this.onKeyUp, this); 90 // 预制体prefab 91 动态创建一些内容时候需要将内容制作成预制体,代码内实例化 92 预制体只能保存自己节点内的东西,节点外的不予保存 93 // 定时器 94 this.schedule(this.func, 1); // 循环执行 1秒1次 95 this.scheduleOnce(this.func, 0); // 执行一次 这种写法会延迟一帧执行 当第二个参数为0时,这样可以实现让代码在所有update执行后执行一次,类似lateUpdate,用于顺序上的控制 96 this.unSchedule(this.func) // 停一个 97 this.unScheduleAll // 停全部 98 // 全局变量的几种方式 99 //1、window.xxx 100 window.globalVar = "xxxx"; 101 //2、module.exports & require(这种是方便的用法) 102 创建一个文件global-module.js 代码如下 103 let arr = [1, 2, 3]; 104 let num = 2; 105 let string = "xxx"; 106 107 module.exports = { 108 arr, 109 num, 110 string 111 } 112 // 或者这么写 113 module.exports = { 114 arr : arr, 115 num : num, 116 string : string 117 } 118 在使用的地方require 119 let globalModule = require('global-module'); 120 globalModule.arr[0] 121 122 //3、statics 静态值 代码如下 只在一个类内共用 123 let Component3 = cc.Class({ 124 extends. cc.Component, 125 propertties : { 126 127 }, 128 statics : { // 静态字段 只实例化一次 129 staticsVar : 'hello', 130 }, 131 132 onLoad : function(){ 133 Component3.staticsVar 134 }, 135 }); 136 137 //4、addPersistRootNode 变为常驻节点 138 cc.game.ddPersistRootNode(this.node) // 不用的时候remove 139 140 //5、cc.sys.localStorage.getItem cc.sys.localStorage.setItem // 把数据写入文件内,重新开游戏数据还在 141 cc.sys.localStorage.setItem('key', 'values'); // 储存 142 cc.sys.localStorage.getItem('key'); // 获取 143 // 坐标转换 144 // 从全局转到本地 145 // 关键,让点和坐标原点配套(原点从屏幕左下角开始0, 0) 146 let locationOfThisNode = this.node.convertToNodeSpaceAR(e.getLocation()); // 转到这个节点坐标系下 147 let locationOfThisNodeParent = this.node.parent.convertToNodeSpaceAR(e.getLocation()); // 转到与这个节点同级,就用parent 148 this.node.position = locationOfThisNodeParent; // 点到哪把节点放在哪的效果 149 150 // 代码查找节点 151 cc.find('node1/').on('touchstart', this.onTouchStart, this); 152 cc.find('node1/node1_1<cc.Sprite>'); // 找到节点3的精灵组件 153 154 155 // 碰撞 156 157 // 动画 158 159 // 组件 Label Sprite cc.Component.Handler tileMap(瓦片) 160 161 162 // 引擎基础