object:dispatchEvent()

Type Function
Object EventDispatcher
Return value none
Revision Release 2022.3683
Keywords dispatchEvent, dispatch event, custom events
See also object:addEventListener()
object:removeEventListener()

Overview

Dispatches specified event to object. The event parameter must be a table with a name property which is a string identifying the type of event. This method is available for any DisplayObject or the global Runtime object, if it has a listener registered to receive name events. We recommend you also include a target property in your event to the event so that your listener can know which object received the event.

In Corona, you can register custom events with both DisplayObjects or the global Runtime object. In both cases, you will have to manually dispatch the event yourself using this object method.

Syntax

object:dispatchEvent( event )
event (required)

Table. Contains event properties. The table must contain a name property that corresponds to the event you wish to dispatch.

Example

-- Create an object that listens to events
local image = display.newImage( "image.png" )
 
-- Setup listener
local myListener = function( event )
        print( "Event " .. event.name )
        print( "Target has width: " .. event.target.contentWidth )
end
 
image:addEventListener( "myEventType", myListener )
 
-- Sometime later, create an event and dispatch it
local event = { name="myEventType", target=image }
image:dispatchEvent( event )