Cocos[0223]

初接触下来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),
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值