在Gideros,我正在利用自定义事件来更新分数。我为事件的接收者提供了以下代码(为简洁起见,省略了一些代码):
GameInfoPanel = Core.class(Sprite)
function GameInfoPanel:init()
self:addEventListener("add_score", self.onAddScore, self) -- Registering event listener here
self.score = 0
end
function GameInfoPanel:onAddScore(event)
self.score = self.score + event.score -- << This line is never reached
end
这是触发事件的代码:
local score_event = Event.new("add_score")
score_event.score = 100
self:dispatchEvent(score_event)
但是,永远不会达到注册为上述监听器的功能。
答案 0 :(得分:1)
好的,我在Gideros Mobile论坛上找到了答案:http://giderosmobile.com/forum/discussion/4393/stuck-with-simple-custom-event/p1
在那里,用户ar2rsawseen表示发件人和收件人必须通过一些共同的对象进行通信(不确定如何或为什么,但它确实有效),所以以下代码实际上对我有用:
GameInfoPanel = Core.class(Sprite)
function GameInfoPanel:init()
stage:addEventListener("add_score", self.onAddScore, self) -- 'stage' is common and accessible to both
self.score = 0
end
function GameInfoPanel:onAddScore(event)
self.score = self.score + event.score
end
事件的发件人:
local score_event = Event.new("add_score")
score_event.score = 100
stage:dispatchEvent(score_event)