监听、触摸事件、坐标系转换

Creator的系统事件

分为“节点系统事件”和“全局系统事件”。

节点系统事件:触发在节点上,包括鼠标事件和触摸事件。

全局系统事件:包括键盘和重力传感事件。

需要通过监听的方法来实现。

监听的注册

  1. 节点.on(节点系统事件的枚举类型或事件名, function(event){},target);

  2. 节点.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的坐标系分为世界坐标系和相对坐标系。

世界坐标系以左下角为原点

  1. Node.convertToWorldSpaceAR(Vec2)把相对于该节点的坐标vec2转换为世界坐标系下的坐标。
  2. 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---监听、触摸事件、坐标系转换的更多相关文章

  1. [Swift通天遁地]三、手势与图表-(2)监听手势事件自由拖动图像视图

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播

    -->事件冒泡和捕获-->事件监听-->阻止事件传播 一.事件冒泡和捕获 1.概念:当给子元素和父元素定义了相同的事件,比如都定义了onclick事件,点击子元素时,父元素的oncl ...

  3. JS 中的事件绑定、事件监听、事件委托

    事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...

  4. javascript事件监听与事件委托

      事件监听与事件委托 在js中,常用到element.addEventListener()来进行事件的监听.但是当页面中存在大量需要绑定事件的元素时,这种方式可能会带来性能影响.此时,我们可以用事件 ...

  5. 在Javascript中监听flash事件(转)

    在Javascript中监听flash事件,其实有两种做法: 1.在特定的环境下(例如专门制作的flash),大家约定一个全局函数,然后在flash的事件中用ExternalInterface.cal ...

  6. Fragment中监听onKey事件,没你想象的那么难。

    项目中越来越多的用到Fragment,在用Fragment取代TabHost的时候遇到了一个问题,我们都知道,TabHost的Tab为Activity实例,有OnKey事件,但是Fragment中没有 ...

  7. js 事件监听 冒泡事件

    js 事件监听  冒泡事件   的取消 [自己写框架时,才有可能用到] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...

  8. 原 JS监听回车事件

    原 JS监听回车事件 发表于2年前(2014-06-04 10:16)   阅读(6101) | 评论(0) 11人收藏此文章, 我要收藏 赞0 1月16日厦门 OSC 源创会火热报名中,奖品多多哦  ...

  9. Android EditText截获与监听输入事件

      Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...

  10. 两种js监听滚轮事件的方式

    前段时间在写前端的时候,需要监听浏览器的滚轮事件 网上查了一下,找到两种监听滚轮事件的方法: 一.原生js通过window.onscroll监听 //window.onscroll = functio ...

随机推荐

  1. css文字颜色渐变的3种实现

    在web前端开发过程中,UI设计师经常会设计一些带渐变文字的设计图,在以前我们只能用png的图片来代替文字,今天可以实现使用纯CSS实现渐变文字了.下面就介绍3中实现方式供大家参考! 基础样式: .g ...

  2. zTree -- jQuery 树插件 使用方法与例子

    简介 zTree 是一个依靠 jQuery 实现的多功能 "树插件". 网址:http://www.ztree.me/v3/main.php#_zTreeInfo 上面的网址里有z ...

  3. 讲清楚之 javascript 参数传值

    讲清楚之 javascript 参数传值 参数传值是指函数调用时,给函数传递配置或运行参数的行为,包括通过call.apply 进行传值. 在实际开发中,我们总结javascript参数传值分为基本数 ...

  4. rabitmq 登录报错:User can only log in via localhost

    安装教程参考:https://blog.csdn.net/qq_43672652/article/details/107349063 修改了配置文件仍然报错,无法登录.解决办法:新建一个用户登录: 查 ...

  5. 【Android开发】控件外边框自定义

    1.在drawable里面新建自定义的资源文件shape <?xml version="1.0" encoding="utf-8"?> <sh ...

  6. 安装并使用Junit

    在Eclipse中配置Junit的方法有两种方式: 第一种方法: 1.下载junit的jar包,目前它的版本是junit3.8.1,可以从www.junit.org上下载. 2.在要使用Junit的p ...

  7. 将子分支代码merge到主分支master分支

    1.首先切换到子分支: git checkout develop2.使用git pull 把分支代码pull下来: git pull3.切换到主分支: git checkout master4.把分支 ...

  8. spring原始注解(value)-03

    本博客依据是是spring原始注解-02的代码 注入普通数据类型:@Value注解的使用 1.添加driver属性,使用value注解 @Service("userService" ...

  9. 用来创建用户docker registry认证的Secret

    集群环境:1.k8s用的是二进制方式安装2.操作系统是linux (centos)3.操作系统版本为 7.4/7.94.k8s的应用管理.node管理.pod管理等用rancher.k8s令牌以及ma ...

  10. 如何使用Android可视化埋点

    Android可视化埋点是Android全埋点的增强.开发者可以将App界面同步至DTM界面,并在DTM界面通过可视化点击的方式添加埋点事件.目前Android可视化埋点包含两种埋点方式:普通可视化埋 ...