1

在 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) 

但是,永远无法到达上面注册为侦听器的函数。

4

1 回答 1

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) 
于 2015-08-14T05:40:48.650 回答