1

In Gideros, I have am making use of a custom event to update the score. 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.

mydoghasworms
  • 18,233
  • 11
  • 61
  • 95

1 Answers1

1

OK, I found an answer on the Gideros Mobile forums: 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:

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) 
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95