初接触下来cocoscreator 跟U3D类似。脚本挂在组件下,this.node
代表这个组件,在properties里面写入
label: {
default: null,
type: cc.Label
},
表示定义了一个类型为Label的文本,然后在组件的属性检查器里面拖动想要关联的那个文本组件,用来跟这个脚本相关联。
等同于用代码获取一个组件
this.label = cc.find("Canvas/Label").getComponent(cc.Label);
添加监听事件
- 方法一:
this.node.on(cc.Node.EventType.TOUCH_END,function(event){
cc.log("inininin");
}, this);
- 方法二:
在properties里面定义
button: {
default: null,
type: cc.Button
}
之后在onLoad里面添加
this.button.node.on('click', this.callback, this);
cocos跟egret不同的是后面的this不是必须加的
this.node.on("touchend",function (event){
console.log("touchend event");
}); // 这样也是可以的
this.node.on("touchend",function (event){
console.log("touchend event");
},this); // 这是加this的
this.node.on("touchend",function (event){
console.log("touchend event");
}.bind(this));//bind是创建了一个新函数而不是修改一个函数.新函数的行为和原来函数的行为一样,但他的接收者是我们给定的对象,而原有函数的接收者保持不变. 好的前面全是我百度的,所以大概我理解为保持this不变
egret的once监听事件这里也是通用的
this.node.once("touchend",function (event){
console.log("touchend event");
}); // 依旧可以不用后面加this
声明属性
在properties里面声明属性,可以在属性检查器里面可视化的更改数值。(感觉超级方便啊,算是一大优点)
properties: {
userID: 20,
userName: "Foobar",
}
properties: {
height: 20,
target: cc.Node,
ints: [cc.Integer],
any: [], //不定义具体类型的数组
color: new cc.Color(255, 255, 255, 128),
}