// 拿到当前组件所依附的节点
this.node
// 拿到当前组件
this
// 如何取得其他节点
// 取得节点的父节点
this.node.parent
// 取得节点的子节点数组
this.node.children
// 通过节点名字取得子节点
this.node.getChildByName('node-3');
// 删除节点
this.node5.removeFromParent(); // 这个只是断开与父节点的链接 并没删除
this.node5.destroy(); // 真正的删除
// 实例化一个节点
let nodeCopy = cc.instantiate(this.node5Prefab);
this.node.addChild(nodeCopy);
// 动作、动画
this.node.runAction(
cc.moveTo(1, 20, 20);
cc.moveTo(1, 20, 20).easing(cc.easeBackIn(3));
cc.rotateTo(1, 180);
cc.fadeOut(1);
cc.scaleTo(1, 2, 2);
cc.removeSelf(true || false);
);
// 事件
// 事件监听 node.on node.once
this.node4.on('枪声', this.rush, this.node4);
this.node5.on('枪声', this.escape, this.node5);
rush : function(e){
this // this.node4
e.detail // 参数{hello : 'world'}
this.runAction(cc.moveTo(1, 200, 0));
}
escape : function(){
this // this.node5
}
// 关闭监听
node.off
// 发射事件
emit 和 dispatchEvent。
两者的区别是,dispatchEvent可以做事件传递(冒泡传送)
this.node.emit('枪声', {hello : 'world'})
// 触摸事件
this.node.on('touchstart', this.onTouchStart, this);
this.node.on('touchmove', this.onTouchMove, this);
this.node.on('touchend', this.onTouchEnd, this);
this.node.on('touchcancel', this.onTouchCancel, this);
onTouchStart : function(e){
console.log('onTouchStart', e);
console.log(e.getLocation());
}
onTouchMove : function(e){
console.log('onTouchMove', e);
}
onTouchEnd : function(e){
console.log('onTouchEnd', e);
}
onTouchCancel : function(e){
console.log('onTouchCancel', e);
}
// 鼠标事件
this.node.on('mouseup', this.mouseup, this);
this.node.on('mousemove', this.mousemove, this);
this.node.on('mousedown', this.mousedown, this);
this.node.on('mouseenter', this.mouseenter, this);
this.node.on('mouselevel', this.mouselevel, this);
this.node.on('mousewheel', this.mousewheel, this);
//键盘事件
cc.systemEvent.on('keydown', this.onKeyDown, this);
onKeyDown : function(e){
if(e.keyCode == cc.KEY.w){
console.log('key w press');
}
}
cc.systemEvent.on('keyup', this.onKeyUp, this);
// 预制体prefab
动态创建一些内容时候需要将内容制作成预制体,代码内实例化
预制体只能保存自己节点内的东西,节点外的不予保存
// 定时器
this.schedule(this.func, 1); // 循环执行 1秒1次
this.scheduleOnce(this.func, 0); // 执行一次 这种写法会延迟一帧执行 当第二个参数为0时,这样可以实现让代码在所有update执行后执行一次,类似lateUpdate,用于顺序上的控制
this.unSchedule(this.func) // 停一个
this.unScheduleAll // 停全部
// 全局变量的几种方式
//1、window.xxx
window.globalVar = "xxxx";
//2、module.exports & require(这种是方便的用法)
创建一个文件global-module.js 代码如下
let arr = [1, 2, 3];
let num = 2;
let string = "xxx";
module.exports = {
arr,
num,
string
}
// 或者这么写
module.exports = {
arr : arr,
num : num,
string : string
}
在使用的地方require
let globalModule = require('global-module');
globalModule.arr[0]
//3、statics 静态值 代码如下 只在一个类内共用
let Component3 = cc.Class({
extends. cc.Component,
propertties : {
},
statics : { // 静态字段 只实例化一次
staticsVar : 'hello',
},
onLoad : function(){
Component3.staticsVar
},
});
//4、addPersistRootNode 变为常驻节点
cc.game.ddPersistRootNode(this.node) // 不用的时候remove
//5、cc.sys.localStorage.getItem cc.sys.localStorage.setItem // 把数据写入文件内,重新开游戏数据还在
cc.sys.localStorage.setItem('key', 'values'); // 储存
cc.sys.localStorage.getItem('key'); // 获取
// 坐标转换
// 从全局转到本地
// 关键,让点和坐标原点配套(原点从屏幕左下角开始0, 0)
let locationOfThisNode = this.node.convertToNodeSpaceAR(e.getLocation()); // 转到这个节点坐标系下
let locationOfThisNodeParent = this.node.parent.convertToNodeSpaceAR(e.getLocation()); // 转到与这个节点同级,就用parent
this.node.position = locationOfThisNodeParent; // 点到哪把节点放在哪的效果
// 代码查找节点
cc.find('node1/').on('touchstart', this.onTouchStart, this);
cc.find('node1/node1_1<cc.Sprite>'); // 找到节点3的精灵组件
// 碰撞
// 动画
// 组件 Label Sprite cc.Component.Handler tileMap(瓦片)
// 引擎基础