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 ...
随机推荐
- STM32 之 HAL库(固件库)
1 STM32的三种开发方式 通常新手在入门STM32的时候,首先都要先选择一种要用的开发方式,不同的开发方式会导致你编程的架构是完全不一样的.一般大多数都会选用标准库和HAL库,而极少部分人会通过直 ...
- 【weex开发】vue-swipe 滑动组件的使用
一,vue-swipe简介 vue-swipe 是饿了么团队开发的vue专用的轮播图插件: 可以实现简单的图片和view轮播,可控制动画时长,可限制手动滑动: 简而言之,可以实现轮播,也可以实现ppt ...
- Android Studio 异常以及解决方案
1. Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVER ...
- vue 相关问题整理
- 使用 Jenkins 进行持续集成与发布流程图-图解
- 如何在 Java 中实现最小生成树算法
定义 在一幅无向图 \(G=(V,E)\) 中,\((u, v)\) 为连接顶点 \(u\) 和顶点 \(v\) 的边,\(w(u,v)\) 为边的权重,若存在边的子集 \(T\subseteq E\ ...
- Sql递归查询,Sqlserver、Oracle、PG、Mysql
递归分两种:一种由父项向下级递归,另一种是由子项向上级递归.下面就这两种情况做个简单的处理. 假设有一个表treeview,包含字段 id,parentid,text 分别代表id,上级id,描述字段 ...
- vue3响应式模式设计原理
vue3响应式模式设计原理 为什么要关系vue3的设计原理?了解vue3构建原理,将有助于开发者更快速上手Vue3:同时可以提高Vue调试技能,可以快速定位错误 1.vue3对比vue2 vue2的原 ...
- spring4+springmvc+springdataJPA+hibernate4+Junit4整合懒加载问题
文章目录 技术交流 #摘要 本文主要是为了解决"spring4+springmvc+springdataJPA+hibernate4+junit4整合",注解了OneToMany. ...
- *CTF babyarm内核题目分析
本文从漏洞分析.ARM64架构漏洞利用方式来讨论如何构造提权PoC达到读取root权限的文件.此题是一个ARM64架构的Linux 5.17.2 版本内核提权题目,目的是读取root用户的flag文件 ...


