Event Listener Method Returning Nil When Being Called

I am following the tutorial on how to create a custom event as shown in the link: https://coronalabs.com/blog/2012/06/26/how-to-use-custom-events-in-corona/

I copied the code directly from the article yet I’m receiving an error. No matter what I tried to do the ‘addEventListener’ method keeps returning nil. Any solution would be much appreciated.

player = {} local event = { name = "health", type = "full", healthPercent = 100 } function health\_listener( event ) if event.type == "full" then print( event.healthAmount ) end end player:addEventListener( "health", health\_listener )

You forgot this part

player:dispatchEvent( event )

Put it below the event table

I have and that doesn’t fix it.

the “player” has to an object (not a table)

try this:

local player = display.newRect(display.contentCenterX, display.contentCenterY, 50, 50)

local event = {

name = “health”,

type = “full”,

healthAmount = 100

}

player:dispatchEvent( event )

function health_listener( event )

  if event.type == “full” then

  print( event.healthAmount )

  end

end

player:addEventListener( “health”, health_listener )

player:dispatchEvent( event )

Does it always have to be a display object to create a custom event? Or could a scene object be used for custom event?

In docs it says runtime or display object
https://docs.coronalabs.com/api/type/EventListener/dispatchEvent.html

You forgot this part

player:dispatchEvent( event )

Put it below the event table

I have and that doesn’t fix it.

the “player” has to an object (not a table)

try this:

local player = display.newRect(display.contentCenterX, display.contentCenterY, 50, 50)

local event = {

name = “health”,

type = “full”,

healthAmount = 100

}

player:dispatchEvent( event )

function health_listener( event )

  if event.type == “full” then

  print( event.healthAmount )

  end

end

player:addEventListener( “health”, health_listener )

player:dispatchEvent( event )

Does it always have to be a display object to create a custom event? Or could a scene object be used for custom event?

In docs it says runtime or display object
https://docs.coronalabs.com/api/type/EventListener/dispatchEvent.html