1. 程式人生 > >cocos2dx-lua捕獲用戶touch事件的幾種方式

cocos2dx-lua捕獲用戶touch事件的幾種方式

listener 創建 父節點 wall AC gets 多點 coord swa

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)

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];
來打開多點觸摸

cocos2dx-lua捕獲用戶touch事件的幾種方式