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