Cocos---监听、触摸事件、坐标系转换
监听、触摸事件、坐标系转换
Creator的系统事件
分为“节点系统事件”和“全局系统事件”。
节点系统事件:触发在节点上,包括鼠标事件和触摸事件。
全局系统事件:包括键盘和重力传感事件。
需要通过监听的方法来实现。
监听的注册
节点.on(节点系统事件的枚举类型或事件名, function(event){},target);
节点.on(节点系统事件的枚举类型或事件名, this.函数名,target);函数名(event){}
(1)使用枚举类型来注册
node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
console.log('Mouse down');
}, this);
(2)使用事件名来注册
node.on('mousedown', function (event) {
console.log('Mouse down');
}, this);
Target:绑定响应函数的调用者。
This---谁点击,函数中this指的是谁。等价于函数上加bind(this)。
某节点---在函数中的this指的是某节点,而不是发生点击的节点。
例子
red和white在属性列表中定义,脚本节点
this.red.on('mousedown',function(){
console.log("red");
console.log(this);
},this);//this----指向当前这个类,如果脚本挂在某个节点上,this.node相当于指向这个节点
// 等价于下面的写法
this.red.on('mousedown',function(){
console.log("redred");
console.log(this);
}.bind(this));
this.white.on('mousedown',function(){
console.log("white");
console.log(this.name);//this相当于this.red,所以不能写this.node.name
},this.red);
监听的关闭
当监听不需要的时候,一定要关闭,消耗资源比较大。
必须把回调函数单独写。
关闭监听的格式
This.node.on(‘’,this.函数名,this);
This.node.off(‘’,this.函数名,this);
函数名(){ }----写在生命周期接口外
例子
start () {
//关闭监听
this.red.on('mousedown',this.redclick,this);
this.white.on('mousedown',function(){
this.red.off('mousedown',this.redclick,this);
},this)
}
redclick(){
console.log("red");
}
移除目标上的所有注册事件。
This.node.targetOff(this)
【例】
this.red.on('mousedown',function(){console.log('down')},this);
this.red.on('mouseup',function(){console.log('up')},this);
this.white.on('mousedown',function(){
console.log('white');
this.red.targetOff(this);
},this)
触摸事件
触摸事件在移动平台和桌面平台都会触发,只需要监听触摸事件即可同时响应移动平台的触摸事件和桌面端的鼠标事件。
例子
@property(cc.Node)
white: cc.Node = null;//注意定义为Node和Sprite类型,注册监听的区别
onLoad () {
this.white.on("touchstart",function(t){
console.log('touchstart');
},this);
this.white.on('touchmove',function(t){
console.log('touchmove');
},this);
this.white.on('touchend',function(t){
console.log('touchend');
},this);
this.white.on('touchcancel',function(t){
console.log('touchcancel');
},this);
}
cc.node其他的事件
触摸事件常用API应用案例
获取触摸位置
可以通过回调函数的参数t获取触摸位置,此坐标是一个世界坐标系下的vec3对象。
坐标空间
Creator的坐标系分为世界坐标系和相对坐标系。
世界坐标系以左下角为原点
- Node.convertToWorldSpaceAR(Vec2)把相对于该节点的坐标vec2转换为世界坐标系下的坐标。
- Node.convertToNodeSpaceAR(Vec2)把世界坐标系下的坐标vec2转换为相对于节点node坐标空间的的坐标。
凡是有AR的,以锚点作为中心点。
获取触摸位置,实现拖动一个对象。
this.red.on('touchmove',function(t){
let p = this.node.convertToNodeSpaceAR(t.getLocation());
this.red.x = p.x;
this.red.y = p.y;
},this)
【例】
@property(cc.Node)
white: cc.Node = null;//注意定义为Node和Sprite类型,注册监听的区别
@property(cc.Node)
blue:cc.Node = null;
@property(cc.Node)
red:cc.Node = null;
onLoad () {
//把blue相对白色节点的坐标转换为世界坐标
let v1 = this.blue.convertToWorldSpaceAR(this.blue.position);
console.log(v1.x,v1.y);
//white的触摸事件监听
this.white.on("touchstart",function(t){
console.log('touchstart');
},this);
this.white.on('touchmove',function(t){
console.log('touchmove');
},this);
this.white.on('touchend',function(t){
console.log('touchend');
},this);
this.white.on('touchcancel',function(t){
console.log('touchcancel');
},this);
//转换触点的坐标空间
this.node.on('touchmove',function(t){
let touch_position = t.getLocation();
// console.log(touch_position.x,touch_position.y);
//转换为相对于canvas节点空间的坐标,以锚点为参考点。
let position = this.node.convertToNodeSpaceAR(touch_position);
console.log(position.x,position.y);
},this);
//通过获取触摸位置,实现拖动一个对象
this.red.on('touchmove',function(t){
let p = this.node.convertToNodeSpaceAR(t.getLocation());
this.red.x = p.x;
this.red.y = p.y;
},this)
}
Vec3的相关API
https://docs.cocos.com/creator/api/zh/classes/Vec3.html?h=向量长度
Cocos---监听、触摸事件、坐标系转换的更多相关文章
- [Swift通天遁地]三、手势与图表-(2)监听手势事件自由拖动图像视图
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播
-->事件冒泡和捕获-->事件监听-->阻止事件传播 一.事件冒泡和捕获 1.概念:当给子元素和父元素定义了相同的事件,比如都定义了onclick事件,点击子元素时,父元素的oncl ...
- JS 中的事件绑定、事件监听、事件委托
事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...
- javascript事件监听与事件委托
事件监听与事件委托 在js中,常用到element.addEventListener()来进行事件的监听.但是当页面中存在大量需要绑定事件的元素时,这种方式可能会带来性能影响.此时,我们可以用事件 ...
- 在Javascript中监听flash事件(转)
在Javascript中监听flash事件,其实有两种做法: 1.在特定的环境下(例如专门制作的flash),大家约定一个全局函数,然后在flash的事件中用ExternalInterface.cal ...
- Fragment中监听onKey事件,没你想象的那么难。
项目中越来越多的用到Fragment,在用Fragment取代TabHost的时候遇到了一个问题,我们都知道,TabHost的Tab为Activity实例,有OnKey事件,但是Fragment中没有 ...
- js 事件监听 冒泡事件
js 事件监听 冒泡事件 的取消 [自己写框架时,才有可能用到] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...
- 原 JS监听回车事件
原 JS监听回车事件 发表于2年前(2014-06-04 10:16) 阅读(6101) | 评论(0) 11人收藏此文章, 我要收藏 赞0 1月16日厦门 OSC 源创会火热报名中,奖品多多哦 ...
- Android EditText截获与监听输入事件
Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...
- 两种js监听滚轮事件的方式
前段时间在写前端的时候,需要监听浏览器的滚轮事件 网上查了一下,找到两种监听滚轮事件的方法: 一.原生js通过window.onscroll监听 //window.onscroll = functio ...
随机推荐
- BGD(批量梯度下降)--学习笔记
函数凸凹性: 用到的范数知识: 详细解释:每一个元素的平方再开方.补充损失函数: Huber Loss知识点 loss函数可以通过loss参数进行设置.SGDRegressor支持以下的loss函数: ...
- 创建TypeScript代码模板(NVS+Yarn+ESLint+Prettier+Husky)
创建TypeScript代码模板(NVS+Yarn+ESLint+Prettier+Husky) Cui, Richard Chikun 本文笔者将带你在Github代码仓库创建TypeScript代 ...
- JavaScript 工作原理之六-WebAssembly 对比 JavaScript 及其使用场景
原文请查阅这里,略有改动,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第六章. 现在 ...
- idea怎么创建python项目
前言 python是一种功能强大和适用面很广的开发语言,在大数据应用和机器学习日益流行的年代,python凭借其简洁.易用和可扩展性获得很多用户的支持,近年来使用率高速增长.python环境下,集成了 ...
- Python入门-内置对象函数
1.callable() 查看函数知否可调用,可调用返回True,不可用返回False print("input函数:", callable(input)) #input函数: T ...
- 申请百度翻译API
申请百度翻译API 0x00 前期准备 百度账号 md5的相关知识 0x01 进入百度开放平台,登录你的百度账号 找到 产品服务 -> 通用翻译API 0x02 点击下面的立即使用按钮,注册成为 ...
- Struts2-day1总结
1.Struts2的基本执行流程(详见我的博客) 2.Struts2的分模块开发 如果有多个项目的Struts.xml写在一起,容易造成数据混乱,所以可以使用分模块的方法,在项目下新建*.xml配置文 ...
- ip地址后面斜杠加数字的含义
案例如:10.121.246.8/29 1.概念: 首先这是一种划分ip的表示方式,叫做无分类域间路由选择(CIDR),区分于传统的划分ip方式(分类的ip划分,在这之后提出了划分子网,即将主机号借出 ...
- 内网穿透NPS
内网穿透实现 nps文档 https://ehang-io.github.io/nps/#/install nps docker镜像 https://hub.docker.com/r/ffdfgdfg ...
- allure用法(一)-配置信息及基本用法
allure是一个轻量级的,灵活的,支持多语言的测试报告工具 优点: 可以为dev/qa 提供 详尽的测试报告.测试步骤.日志 可以为管理层提供更好的统计报告 Java语言开发的 可以集成到jenki ...