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];
本文摘抄自:https://www.cnblogs.com/lcchuguo/p/5157888.html
本人只是看這篇文章講解清晰,以此記錄、學習!