How to add an event to the composer screen and how to dispatch it?

Hi,

I have created a new scene using the composer.  But how do I add my own event into it?

I tried the following,

-- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) scene:addEventListener("gameEnd", scene) -- this is my own one

Then how do I call it from another object?   I have a character object, when the character dies I want to dispatch an event to say to dispatch “gameEnd” event

Composer is not supposed to work that way. You should create a new composer lua file (you can call it game-end.lua) and then call composer.gotoScene(“game-end”, params).

You can read more here:  http://docs.coronalabs.com/api/library/composer/index.html

Renato,

if my gameEnd listener handler that’s exactly what I have…

But the issue I am having is how do I call the event?  The event should happen when the character object dies… when the character dies I want to dispatch the event, which in turn will call the event handler…

I hope that makes sense

I understand that you already have a function called gameEnd, is that right? So just call this function when the characters dies. You don’t have to add events.

You can dispatch custom events if you want. See http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

So when your character dies you could perhaps do:

[lua]

scene:dispatchEvent( { name=“gameEnd”, target=characterGroup } )

[/lua]

Personally I would make a character module and do something like:

[lua]

– character.lua

local M = display.newGroup()

– Add your stuff to this group

local function died()

   M:dispatchEvent({name = “gameEnd”})

end

return M

[/lua]

[lua]

– Main gama lua file

local character = require(character)

character:addEventListener(“gameEnd”, someFunction) 

[/lua]

If you want to get really fancy: http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

jonjonsson,

I am already using OO with the middleclass library… and yeah I already have a separate class for the character

I am using the composer for scene management

local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE -- unless "composer.removeScene()" is called. --------------------------------------------------------------------------------- -- local forward references should go here --------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view self.character = new character(self.view) -- creating character -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) scene:addEventListener("gameEnd", scene) -- this is my own one --------------------------------------------------------------------------------- return scene

The issue I was having was that my character module did not know about the “gameEnd” event…

But I think the issue might be I put the event handlder in the wrong place in the scene template…

I might need to put the Add the listener in the scene:create

I will try it out later today and let you know…

Composer is not supposed to work that way. You should create a new composer lua file (you can call it game-end.lua) and then call composer.gotoScene(“game-end”, params).

You can read more here:  http://docs.coronalabs.com/api/library/composer/index.html

Renato,

if my gameEnd listener handler that’s exactly what I have…

But the issue I am having is how do I call the event?  The event should happen when the character object dies… when the character dies I want to dispatch the event, which in turn will call the event handler…

I hope that makes sense

I understand that you already have a function called gameEnd, is that right? So just call this function when the characters dies. You don’t have to add events.

You can dispatch custom events if you want. See http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

So when your character dies you could perhaps do:

[lua]

scene:dispatchEvent( { name=“gameEnd”, target=characterGroup } )

[/lua]

Personally I would make a character module and do something like:

[lua]

– character.lua

local M = display.newGroup()

– Add your stuff to this group

local function died()

   M:dispatchEvent({name = “gameEnd”})

end

return M

[/lua]

[lua]

– Main gama lua file

local character = require(character)

character:addEventListener(“gameEnd”, someFunction) 

[/lua]

If you want to get really fancy: http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

jonjonsson,

I am already using OO with the middleclass library… and yeah I already have a separate class for the character

I am using the composer for scene management

local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE -- unless "composer.removeScene()" is called. --------------------------------------------------------------------------------- -- local forward references should go here --------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view self.character = new character(self.view) -- creating character -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) scene:addEventListener("gameEnd", scene) -- this is my own one --------------------------------------------------------------------------------- return scene

The issue I was having was that my character module did not know about the “gameEnd” event…

But I think the issue might be I put the event handlder in the wrong place in the scene template…

I might need to put the Add the listener in the scene:create

I will try it out later today and let you know…