I’m currently in the process of building a UI library, largely because I can’t stand hardcoding UI.
To facilitate events on buttons, updates to UI elements based on game state changes, etc… I’m trying to take advantage of the Corona custom event - but having a few problems.
I have this code in a file called menu.lua:
[lua]
UIManager:addCustomCallback(
function()
--when scoreBtn is clicked it creates a custom dispatch
game.m_oPlayer[“Data”].score = game.m_oPlayer[“Data”].score + 100
UIManager:addCustomDispatch(
{
name = “updateScore”,
--target = object,
score = game.m_oPlayer[“Data”].score
}, “scoreButton”)
end, “scoreButton”)
UIManager:addCustomEventListener(
function(self)
print(“STOP”)
end, “scoreLabel”)
[/lua]
It’s a rather obtuse way of creating some buttons and adding events - however I can now create my interfaces in an editor and extract the data from a JSON file.
The problem comes form the related code in UIManager.lua:
[lua]
function UIManager:addCustomDispatch(event, name)
print(“UIManager: addCustomDispatch (”…json.encode(event)…"/"…name…")")
self.struct[name]:dispatchEvent(event)
--Runtime:dispatchEvent(event)
end
function UIManager:addCustomEventListener(listener, name)
print(“UIManager: addCustomEventListener (”…name…")")
--works with Runtime - doesnt work adding to object
self.struct[name]:addEventListener(“updateScore”, listener)
--Runtime:addEventListener(“updateScore”, listener)
end
function UIManager:addCustomCallback(callback, name)
print(“UIManager: Adding Custom Callback (”…name…")")
self.struct[name].callback = callback
end
[/lua]
The events just aren’t being listened to; however bizarrely if I uncomment the Runtime calls in addCustomDispatch() and addCustomEventListener() it works perfectly?!?
The self.struct is a simple data structure that contains the various display objects that make up the interface; I have checked that they are correct by making a simple change to the text value of the object in question - like self.struct[name].text = Is this working" and the display object that I want to add the listener to changes as expected. So the .struct is referring to the correct display object; however the dispatchEvent or addEventListener isn’t firing?
Any help or advice would be appreciated…