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

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. DIANA算法

    DIANA算法 DIANA算法示例 DIANA算法练习

  2. 顺利通过EMC实验(12)

  3. Canvas 与 SVG

    什么是SVG? 引用w3c的一段话就是: SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SVG 使用 XML 格式定义图形 ...

  4. python爬虫---链家网二手房价采集

    代码: import requests from lxml import etree import pandas as pd from pyecharts.charts import Bar from ...

  5. java的命令行参数到底怎么用,请给截图和实际的例子

    8.2 命令行参数示例(实验) public class Test {    public static void main(String[] args){        if(args.length ...

  6. 142. 环形链表 II

    做题思路 or 感想 : 1,这一题用快慢指针来判断是否有环,快慢指针同一起点,速度不同,如果有环,则必定会相遇 2,第二个有意思的点就是数论环节来弄出环入口了,真的太精妙了,但因为我表述能力不好,这 ...

  7. matplotlib---legend图例

    import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3, 3, 50) y1 = 2 * x + 1 y2 = x ...

  8. Java应用工程结构

    分层的本质是关注点分离,隔离对下层的变化,可以简化复杂性,使得层次结构更加清晰. 1. 主流分层结构介绍 目前业界存在两种主流的应用工程结构:一种是阿里推出的<Java开发手册>中推荐的, ...

  9. (转)Angular中的拦截器Interceptor

    什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 转之:http://my.osch ...

  10. 发布nuget包的正确姿势---cicd自动打包发布nuget包

    最轻便的发布nuget包方式,方便cicd自动打包发布nuget包 首先新建项目 项目名随便取,这里就叫它GuiH.ClassLibrary 默认即可,需要改目标版本时,等创建好再改 项目创建好了 随 ...