本文地址:
http://www.cppblog.com/zdhsoft/archive/2016/04/12/213238.html
cocos2dx笔记汇总
用cococstudio编辑的UI,都是带Touch事件的,这样,使得我们编写UI逻辑处理简单很多了,这个是基于lua实现,不需要改cocos2dx引擎.
1localstTopInfoItem=GUIReader:shareReader():widgetFromJsonFile("ui/top_info.json")
2ui.AddListenerByTag(stTopInfoItem,TAG.TOP_RET,TOUCH_EVENT_ENDED,PlaceRandomLayer.OnReturnMain)
cocos2dx笔记汇总
用cococstudio编辑的UI,都是带Touch事件的,这样,使得我们编写UI逻辑处理简单很多了,这个是基于lua实现,不需要改cocos2dx引擎.
1--判断是否是有效的事件类型
2 functionisValidTouchEventType(paramEventType)
3 ifparamEventType==TOUCH_EVENT_ENDEDorparamEventType==TOUCH_EVENT_BEGANorparamEventType==TOUCH_EVENT_MOVEDorparamEventType==TOUCH_EVENT_CANCELEDthen
4 return true
5 else
6 false
7end
8end
9--根据Widget的tag来侦听
10 functionAddListenerByTag(paramWidget,paramTag,paramEventType,paramCallBack)
11 ifnotparamWidgetthen
12cclog("参数paramWidget为nil对象")
13 return
14end
15 ifnotui.isValidTouchEventType(paramEventType)then
16cclog("未知的事件类型:"..paramEventType)
17 18end
19
20localst=ui.getWidgetByTag(paramWidget,paramTag)
21 ifnotstthen
22 23end
24
25local functionOnTouch(paramSender,paramType)
26 ifparamType==paramEventTypethen
27paramCallBack(paramSender,128)">28end
29end
30st:addTouchEventListener(OnTouch)
31end
32
33--根据Widget的Name来侦听
34 functionAddListenerByName(paramWidget,paramWidgetName,128)">35 36cclog("参数paramWidget为nil对象")
37 38end
39 40cclog("未知的事件类型:"..paramEventType)
41 42end
43
44localst=ui.getWidgetByName(paramWidget,paramWidgetName)
45 46 47end
48
49local 50 51paramCallBack(paramSender,128)">52end
53end
54st:addTouchEventListener(OnTouch)
55end
使用实例:
2 functionisValidTouchEventType(paramEventType)
3 ifparamEventType==TOUCH_EVENT_ENDEDorparamEventType==TOUCH_EVENT_BEGANorparamEventType==TOUCH_EVENT_MOVEDorparamEventType==TOUCH_EVENT_CANCELEDthen
4 return true
5 else
6 false
7end
8end
9--根据Widget的tag来侦听
10 functionAddListenerByTag(paramWidget,paramTag,paramEventType,paramCallBack)
11 ifnotparamWidgetthen
12cclog("参数paramWidget为nil对象")
13 return
14end
15 ifnotui.isValidTouchEventType(paramEventType)then
16cclog("未知的事件类型:"..paramEventType)
17 18end
19
20localst=ui.getWidgetByTag(paramWidget,paramTag)
21 ifnotstthen
22 23end
24
25local functionOnTouch(paramSender,paramType)
26 ifparamType==paramEventTypethen
27paramCallBack(paramSender,128)">28end
29end
30st:addTouchEventListener(OnTouch)
31end
32
33--根据Widget的Name来侦听
34 functionAddListenerByName(paramWidget,paramWidgetName,128)">35 36cclog("参数paramWidget为nil对象")
37 38end
39 40cclog("未知的事件类型:"..paramEventType)
41 42end
43
44localst=ui.getWidgetByName(paramWidget,paramWidgetName)
45 46 47end
48
49local 50 51paramCallBack(paramSender,128)">52end
53end
54st:addTouchEventListener(OnTouch)
55end
1localstTopInfoItem=GUIReader:shareReader():widgetFromJsonFile("ui/top_info.json")
2ui.AddListenerByTag(stTopInfoItem,TAG.TOP_RET,TOUCH_EVENT_ENDED,PlaceRandomLayer.OnReturnMain)