简体   繁体   English

Gideros:自定义事件未到达事件侦听器

[英]Gideros: Custom event not reaching event listener

In Gideros, I have am making use of a custom event to update the score. 在Gideros中,我正在使用自定义事件来更新分数。 I have the following code for the receiver of the event (some lines left out for brevity): 我为事件的接收者提供了以下代码(为简洁起见,省略了一些行):

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

And this is the code that triggers the event: 这是触发事件的代码:

      local score_event = Event.new("add_score")
      score_event.score = 100
      self:dispatchEvent(score_event) 

However, the function registered as the listener above is never reached. 但是,从未达到注册为上述侦听器的功能。

OK, I found an answer on the Gideros Mobile forums: http://giderosmobile.com/forum/discussion/4393/stuck-with-simple-custom-event/p1 好的,我在Gideros移动论坛上找到了答案: http ://giderosmobile.com/forum/discussion/4393/stuck-with-simple-custom-event/p1

There, user ar2rsawseen indicates that the sender and receiver must communicate via some common object (not sure about the how or why, but it works), so the following code actually worked for me: 在那里,用户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

And the sender of the event: 以及事件的发送者:

  local score_event = Event.new("add_score")
  score_event.score = 100
  stage:dispatchEvent(score_event) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM