0
votes

How does one do Inter-Object communications (e.g. triggering events) in Corona? Some sample code would be great.

Notes:

  • I see there is a way to create custom events on an object, however it seems you really need a handle to that object before you can trigger (dispatch) it.

  • What I'm interested here is two completely separate objects. Eg say you clicked on a display object on the screen the ability to say in a global event type sense "MyInventoryHasIncreased", and then have any other display objects that need to subscribe to this pick it up and adjust their display accordingly. Subscribe

1

1 Answers

2
votes

If you ever need global events, think about using the Runtime:addEventListener().

local function invIncreased(event)
    print("handle inventory increase from" .. event.target)
end

local image = display.newImage("image.png") 
Runtime:addEventListener("MyInventoryHasIncreased", invIncreased)

local event = {name="MyInventoryHasIncreased", target=image} 
Runtime:dispatchEvent(event)

You can split the Runtime:addEventListener from the Runtime:dispatchEvent to get your desired object to object communication.