这里仅仅针对lua

1.为每一个关心的事件注冊回调函数
详细分为下面几种
1>单点触摸
注冊函数为
cc.Handler.EVENT_TOUCH_BEGAN      = 40
cc.Handler.EVENT_TOUCH_MOVED      = 41
cc.Handler.EVENT_TOUCH_ENDED      = 42
cc.Handler.EVENT_TOUCH_CANCELLED  = 43
注冊的时候必须通过
cc.EventListenerTouchOneByOne:create() 创建listener
onTouchBegin/onTouchMove/onTouchEnd为自己注冊的回调函数
代码例如以下:
local listener = cc.EventListenerTouchOneByOne:create();
listener:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCH_BEGAN);
listener:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCH_MOVED);
listener:registerScriptHandler(onTouchEnd,cc.Handler.EVENT_TOUCH_ENDED);


2>多点点触摸
cc.Handler.EVENT_TOUCHES_BEGAN    = 44
cc.Handler.EVENT_TOUCHES_MOVED    = 45
cc.Handler.EVENT_TOUCHES_ENDED    = 46
cc.Handler.EVENT_TOUCHES_CANCELLED = 47
注冊的时候必须通过
cc.EventListenerTouchAllAtOnce:create() 创建listener
onTouchesBegin/onTouchesMove/onTouchesEnd为自己注冊的回调函数
代码例如以下:
local listener = cc.EventListenerTouchAllAtOnce:create();
listener:registerScriptHandler(onTouchesBegin,cc.Handler.EVENT_TOUCHES_BEGAN);
listener:registerScriptHandler(onTouchesMove,cc.Handler.EVENT_TOUCHES_MOVED);
listener:registerScriptHandler(onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED);

最后通过以下的代码绑定listener
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,_layer);
当中_layer是要须要事件的对象
前面 cc.Director:getInstance() 也可用换成_layer 或者 _layer的父节点的对象

这里有几点须要注意:
1.onTouchesBegin/onTouchBegin 里面须要 return true,表示须要处理这个事件,不然不会掉用onTouchMove/onTouchEnd

2.每一个触摸函数都包括2个參数 以onTouchMove为例:
local function onTouchMove(touch,event)
end
touch / event 都是userdata(能够理解为C/LUA共同拥有的数据 仅仅要实现了相应的方法 C/Lua能够直接訪问/赋值/调用函数 由C管理这块内存)

touch 是当前的触摸点 下面是它的方法
/** Returns the current touch location in OpenGL coordinates.
*
* @return The current touch location in OpenGL coordinates.
*/
Vec2 getLocation() const;
/** Returns the previous touch location in OpenGL coordinates.
*
* @return The previous touch location in OpenGL coordinates.
*/
Vec2 getPreviousLocation() const;
/** Returns the start touch location in OpenGL coordinates.
*
* @return The start touch location in OpenGL coordinates.
*/
Vec2 getStartLocation() const;
/** Returns the delta of 2 current touches locations in screen coordinates.
*
* @return The delta of 2 current touches locations in screen coordinates.
*/
Vec2 getDelta() const;
/** Returns the current touch location in screen coordinates.
*
* @return The current touch location in screen coordinates.
*/
Vec2 getLocationInView() const;
/** Returns the previous touch location in screen coordinates. 
*
* @return The previous touch location in screen coordinates.
*/
Vec2 getPreviousLocationInView() const;
/** Returns the start touch location in screen coordinates.
*
* @return The start touch location in screen coordinates.
*/
如以下的代码能够在onTouchMove中直接获取 用户手指的移动距离
local dis = touch:getDelta()
print(dis.x,dis.y);
假设是多点触摸 touch是一个table
touch[1] touch[2] touch[3]…是触摸的相应点

event 能够表明表明
1.当前用户点击了那个object  (event:getCurrentTarget())
2.当前是press/move/release的那个状态 (event:getEventCode())
cc.EventCode =
{
    BEGAN = 0,
    MOVED = 1,
    ENDED = 2,
    CANCELLED = 3,
}
所以我们能够通过一个回调函数来推断当前是那个操作 而不用写3个函数

演示样例代码

local function onTouchBegin(touch,event)
     local p = touch:getLocation();
     p = _layer:convertToNodeSpace(p);
     print(p.x,p.y)
     return true;
end

local function onTouchMove(touch,event)
end

local function onTouchEnd(touch,event)
end

local function onTouchesBegin(touch,event)
    return true;
end

local function onTouchesMove(touch,event)

    for i = 1,table.getn(touch) do  
        local location = touch[i]:getLocation()  
        print(i,location.x,location.y)
    end  
end
local function onTouchesEnd(touch,event)
    print("onTouchesEnd");
end

     _layer = cc.Layer:create();
    _layer:setTouchEnabled(true)
    local listener1 = cc.EventListenerTouchOneByOne:create();
    listener1:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCH_BEGAN);
    listener1:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCH_MOVED);
    listener1:registerScriptHandler(onTouchEnd,cc.Handler.EVENT_TOUCH_ENDED);

   --多点触摸
   -- local listener2 = cc.EventListenerTouchAllAtOnce:create()
   --listener2:registerScriptHandler(onTouchesBegin,cc.Handler.EVENT_TOUCHES_BEGAN )
   -- listener2:registerScriptHandler(onTouchesMove,cc.Handler.EVENT_TOUCHES_MOVED )
   -- listener2:registerScriptHandler(onTouchesEnd,cc.Handler.EVENT_TOUCHES_MOVED )

    local eventDispatcher = _layer:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, _layer)
    --eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, _layer)


2.直接看代码
local function onTouchEvent(state , ... )
        local args = {...};
        print(state);

        for k,v in pairs(args[1]) do
            print(k,v)
        end
    end 

_layer:registerScriptTouchHandler(onTouchEvent,true,0,false);

@onTouchEvent 回调
@ture表示捕获要捕获多点触摸
@0优先级
@false 是否吞没触摸事件
假设是单点触摸 args[1] ->x,y,id
假设是多点触摸 args[1] ->x1,y1,id1,x2,y2,id2

这里cocos2dx-lua封装了 Layer:onTouch(callback, isMultiTouches, swallowTouches)
建议直接使用



这里假设你用cocos2dx-lua 的 lua-empty-test 做模板建立project 有个bug
须要在  didFinishLaunchingWithOptions 加入代码
[eaglView setMultipleTouchEnabled:YES];
来打开多点触摸 这里我浪费了半天事件调试 FUCK










cocos2dx-lua捕获用户touch事件的几种方式的更多相关文章

  1. 生成freeswitch事件的几种方式

    本文描述了生成freeswitch事件的几种方式,这里记录下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 在freeswitch代码中加入事件 ...

  2. Android_安卓为按钮控件绑定事件的五种方式

    一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...

  3. Java添加事件的四种方式

    Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动) /** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author ...

  4. nodejs触发事件的两种方式

    nodejs触发事件的两种方式: 方式之一:通过实例化events.EventEmitter //引入events模块 var events = require('events'); //初始化eve ...

  5. 为input标签绑定事件的几种方式

    为input标签绑定事件的几种方式 1.JavaScript原生态的方式,直接复制下面的代码就会有相应的效果 <!DOCTYPE html><html><head> ...

  6. android点击事件的四种方式

    android点击事件的四种方式 第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; im ...

  7. JS与JQ绑定事件的几种方式.

    JS与JQ绑定事件的几种方式 JS绑定事件的三种方式 直接在DOM中进行绑定 <button onclick="alert('success')" type="bu ...

  8. jQuery绑定事件的四种方式:bind、live、delegate、on

    1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undele ...

  9. jQuery---on注册事件的2种方式

    on注册事件的2种方式 on注册事件的语法 on注册简单事件 // 这个是p自己注册的事件(简单事件) $("p").on("click", function ...

随机推荐

  1. JavaSE学习总结第07天_面向对象2

      07.01 成员变量和局部变量的区别 1.在类中的位置不同 成员变量    类中方法外 局部变量    方法内或者方法声明上 2.在内存中的位置不同 成员变量   堆内存 局部变量   栈内存 3 ...

  2. c++实现二分查找

    简要描述: 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除 困难. 条件:查找的数组必须要为有序数组. 二分查找的过程剩简要描述如下图: 二种实 ...

  3. [LeetCode]题解(python):015-3Sum

    题目来源: https://leetcode.com/problems/3sum/ 题意分析: 这道题目是输入一个数组nums.找出所有的3个数使得这3个数之和为0.要求1.输出的3个数按小到大排序, ...

  4. 我在北京找工作(二):java实现算法<1> 冒泡排序+直接选择排序

    工作.工作.找工作.经过1个多星期的思想斗争还是决定了找JAVA方面的工作,因为好像能比PHP的工资高点.呵呵 :-)  (其实我这是笑脸,什么QQ输入法,模拟表情都没有,忒不人性化了.) 言归正传, ...

  5. Objective-C基础教程读书笔记(7)

    第7章 深入了解Xcode Xcode是一个很好用的工具,有很多强大的功能,不过并不是所有的功能都易于发现.如果你打算长期使用这个强大的工具,就肯定要尽可能多了解它.本章将介绍一些Xcode编辑器的使 ...

  6. 使用CAShapeLayer和UIBezierPath画一个自定义半圆弧button

    通常我们使用系统自带的UIButton时,一般都是Rect矩形形式的,或则美工给出一张半圆弧的按钮,如图为一张半圆加三角形的按钮,而此时,如果给按钮添加点击事件时,响应事件依然为矩形区域,不符合我们的 ...

  7. java字符串输出

    package mytest; public class Mycode { public static void main(String[] args){ String[]seasons = {&qu ...

  8. 一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)

    参考:MFC 程序入口和执行流程  http://www.cnblogs.com/liuweilinlin/archive/2012/08/16/2643272.html 程序MFCFlie      ...

  9. C++ cout 如何保留小数输出

    参考 : http://upliu.net/how-cout-out-2-precision.html 大家都知道用 C 语言中 printf () 函数可以非常方便控制保留 几位小数输出 不过在 C ...

  10. IOS7修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    解决方法 1: 自从IOS7后UINavigationBar的一些属性的行为发生了变化.你可以在下图看到: 现在,如果你要修改它们的颜色,用下面的代码: 1 2 3 4 self.navigation ...