大佬教程收集整理的这篇文章主要介绍了解析Quick-Cocos2d-x中自定义事件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Quick-Cocos2d-x中的事件机制在官方的文档中已经讲解得很清楚了,查看这里。
这些方法能处理绝大多数的事件,但如果要实现自定义的事件(例如我们自己实现一个类,对该类添加自定义的事件处理)就需要对类进行扩展了。
下面讲讲如何使用自定义(扩展)的事件机制。
首先认识一下类EventProxy,就是这个类实现了自定义的消息处理
该类有四个函数,分别是:
addEventListener
removeEventListener
removeAllEventListenersForEvent
removeAllEventListeners
根据名字就能知道这几个函数的作用。
下面以我们定义一个继承自Node的类来了解自定义事件的处理方法
定义类EXNode如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
localEXNode=
class
(
"EXNode"
,function()
return
display.newNode()
end)
functionEXNode:ctor()
cc.ui.UIPushButton.
new
({normal=
"button/button_normal.png"
,pressed=
"button/button_SELEcted.png"
})
:align(display.CENTER,display.cx,display.cy)
:onButtonClicked(function()
self:getChildEvent()
end)
:addTo(self)
end
functionEXNode:getChildEvent()
end
functionEXNode:onEnter()
self:setTouchEnabled(
true
)
end
functionEXNode:onExit()
self:removeAllEventListeners()
end
return
EXNode
|
1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
localEventScene=
class
(
"EventScene"
,function()
return
display.newScene(
"EventScene"
)
end)
functionEventScene:ctor()
self.exNode=EXNode.
new
()
self:addChild(self.exNodE)
end
functionEventScene:onMynews()
printLog(
"INFO"
,
"父Node知道了子Node发送过来的消息"
)
end
functionEventScene:onEnter()
end
functionEventScene:onExit()
end
return
EventScene
|
1
|