这里只针对lua
具体分为以下几种
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
代码如下:
locallistener =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
代码如下:
locallistener =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 functiononTouchMove(touch,event)
end
touch 是当前的触摸点 以下是它的方法
/**ReturnsthecurrenttouchlocationinOpenGLcoordinates.
*
*@returnThecurrenttouchlocationinOpenGLcoordinates.
*/
Vec2getLocation()const;
/**ReturnstheprevIoUstouchlocationinOpenGLcoordinates.
*
*@returnTheprevIoUstouchlocationinOpenGLcoordinates.
*/
Vec2getPrevIoUsLocation()const;
/**ReturnsthestarttouchlocationinOpenGLcoordinates.
*
*@returnThestarttouchlocationinOpenGLcoordinates.
*/
Vec2getStartLocation()const;
/**Returnsthedeltaof2currenttoucheslocationsinscreencoordinates.
*
*@returnThedeltaof2currenttoucheslocationsinscreencoordinates.
*/
Vec2getDelta()const;
/**Returnsthecurrenttouchlocationinscreencoordinates.
*
*@returnThecurrenttouchlocationinscreencoordinates.
*/
Vec2getLocationInView()const;
/**ReturnstheprevIoUstouchlocationinscreencoordinates.
*
*@returnTheprevIoUstouchlocationinscreencoordinates.
*/
Vec2getPrevIoUsLocationInView()const;
/**Returnsthestarttouchlocationinscreencoordinates.
*
*@returnThestarttouchlocationinscreencoordinates.
*/
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,
}
示例代码
localfunctiononTouchBegin(touch,event)
local p =touch:getLocation();
p = _layer:convertToNodeSpace(p);
print(p.x,p.y)
returntrue;
end
localfunctiononTouchMove(touch,event)
end
localfunctiononTouchEnd(touch,event)
end
localfunctiononTouchesBegin(touch,event)
returntrue;
end
localfunctiononTouchesMove(touch,event)
fori=1,table.getn(touch)do
locallocation=touch[i]:getLocation()
print(i,location.x,location.y)
end
end
localfunctiononTouchesEnd(touch,event)
print("onTouchesEnd");
end
_layer=cc.Layer:create();
_layer:setTouchEnabled(true)
locallistener1=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);
--多点触摸
-- locallistener2=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)
localeventDispatcher=_layer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraPHPriority(listener1,_layer)
--eventDispatcher:addEventListenerWithSceneGraPHPriority(listener2,_layer)
2.直接看代码
localfunctiononTouchEvent(state,...)
localargs={...};
print(state);
fork,vinpairs(args[1])do
print(k,v)
end
end
_layer:registerScriptTouchHandler(onTouchEvent,true,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
[eaglViewsetMultipleTouchEnabled:YES];
来打开多点触摸 这里我浪费了半天事件调试 FUCK