Cocos Creator脚本开发(三)
来源:互联网 发布:k11防水涂料与js怎么样 编辑:程序博客网 时间:2024/05/29 13:42
事件处理是在节点(cc.Node)中完成的。对于组件,可以通过访问节点 this.node 来注册和监听事件。监听事件可以 通过 this.node.on() 函数来注册
cc.Class({ extends: cc.Component, properties: { }, onLoad: function () { this.node.on('mousedown', function ( event ) { console.log('Hello!'); }); }, });
事件监听函数 on 可以传第三个参数 target,用于绑定响应函数的调用者。除了使用 on 监听,我们还可以使用 once 方法。once 监听在监听函数响应后就会关闭监听事件。
// 使用函数绑定this.node.on('mousedown', function ( event ) { this.enabled = false;}.bind(this));// 使用第三个参数this.node.on('mousedown', function (event) { this.enabled = false;}, this);
当我们不再关心某个事件时,我们可以使用 off 方法关闭对应的监听事件。需要注意的是,off 方法的 参数必须和 on 方法的参数一一对应,才能完成关闭。
cc.Class({ extends: cc.Component, _sayHello: function () { console.log('Hello World'); }, onEnable: function () { this.node.on('foobar', this._sayHello, this); }, onDisable: function () { this.node.off('foobar', this._sayHello, this); },});
我们可以通过两种方式发射事件:emit 和 dispatchEvent。两者的区别在于,后者可以做事件传递。
cc.Class({ extends: cc.Component, onLoad: function () { this.node.on('say-hello', function (event) { console.log(event.detail.msg); }); }, start: function () { this.node.emit('say-hello', { msg: 'Hello, this is Cocos Creator', }); },});
0 0
- Cocos Creator脚本开发(三)
- Cocos Creator脚本开发(一)
- Cocos Creator脚本开发(二)
- Cocos Creator脚本开发(四)
- cocos Creator 学习(三)
- Cocos Creator 模块化脚本(摘自官方文档)
- Cocos Creator 插件脚本(摘自官方文档)
- 【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合
- 【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合
- 在cocos creator中使用protobufjs(三)
- cocos Creator 学习(二)
- cocos creator 学习(五)
- Cocos脚本开发
- cocos creator
- cocos Creator
- Cocos Creator 脚本执行顺序(摘自官方文档)
- Cocos Creator中模块化脚本(官方文档摘录)
- 打怪物框架示意图(cocos creator)
- Eclipse Error – Failed to load the JNI shared library “C:\Program Files (x86)\Java\jre6\bin\client\j
- 观察者
- Android基于百度API的定位功能
- hibernate使用
- python数字图像处理(19):骨架提取与分水岭算法
- Cocos Creator脚本开发(三)
- 利用fragment transaction实现fragment切换的动画
- Makefile笔记
- Handler的消息派发
- static实现单例的隐患
- 超高速前端开发工具——Emmet
- django遇到css加载不成功的问题
- Android中从A控件长按拖曳到B控件,并且B控件要知道是谁拖曳到它那里。
- List和set集合:交集、差集、合集的区别retainAll,removeAll、addAll