最近整理自己做过的项目,顺便记录下一些有用的代码:
1. 以前做过一个项目有这样的需求: 游戏过程中会触发同层级节点顺序的频繁改变,当时没有找到合适的方法,就写了一个
利用notify 函数实现的方法, 现优化下贴在这里
代码如下
cc.Class({
extends: cc.Component,
//编辑器属性定义
properties: {
zIndex: {
type: cc.Integer, //使用整型定义
default: 0,
//notify用来监听属性值是否变化
notify(oldValue) {
//判断与原来值是否一样
if (oldValue === this.zIndex) {
return;
}
this.node.zIndex = this.zIndex;
}
}
},
onLoad () {
this.node.zIndex = this.zIndex;
}
});
用法: 把此脚本挂到需要改变层级顺序的节点上就可以
调用:this.node.setLocalZOrder(index);