在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) 但是,上面注册为侦听器的函数永远不会被访问。
发布于 2015-08-14 13:40:49
好的,我在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) https://stackoverflow.com/questions/31996012
复制相似问题