cocos2dx-lua捕获用户touch事件的几种方式

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);markdown

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的父节点的对象lua

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

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

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个函数blog

示例代码事件

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

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)

end
local function onTouchesEnd(touch,event)
print(“onTouchesEnd”);
end

for i = 1,table.getn(touch) do  
    local location = touch[i]:getLocation()  
    print(i,location.x,location.y)
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 作模板创建工程 有个bug 须要在 didFinishLaunchingWithOptions 添加代码 [eaglView setMultipleTouchEnabled:YES]; 来打开多点触摸

相关文章
相关标签/搜索