Event listener question

Hi i’m new to Corona and Lua

In my game i have a Group with more buttons in it, when one of them is pressed the game calls a function

myGroup:addEventListener(“touch”, playerAction)​

local function playerAction(event)
  --stuff
end​

but i want the playerAction function to do a different action depending of which of the buttons was pressed, something like:

local function playerAction(event)
  – if button1 was pressed then …
  – if button2 was pressed then …

  – if button3 was pressed then …
  end
end​

Any ideas on how to do it?

When you create buttons add to it new variable

button1.name = “button1”

button2.name = “button2”

and so on. Then in listener you can check which one button trigger event

if ( event.target.name == "button1" ) then   do something end

Should work.

Hope this help :slight_smile:

What you have to do is make sure is you buttons have a unique id, then you could test for phase(optional) and target.id.

Try this:

button1 = widget.newButton{ id = "button1", onEvent = btnRelease } button2 = widget.newButton{ id = "button2", onEvent = btnRelease }

local function btnRelease(event) if (event.phase == "ended" and event.target.id == "button1") then --do stuff end if (event.phase == "ended" and event.target.id == "button2") then --do stuff end end

Edit: Define the local function before this line of code: 

function scene:create(event)

Also, if you check for phase, an event listener is not needed.

Looks like i can’t get this to work

what is “function scene:create(event)” ? i don’t have it in my code

That is code from a Composer scene. If you’re not using Composer to manage multiple screens  you won’t have it.

Rob

This the composer scene code, I recommend you use it, it is very helpful for managing scenes.

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

When you create buttons add to it new variable

button1.name = “button1”

button2.name = “button2”

and so on. Then in listener you can check which one button trigger event

if ( event.target.name == "button1" ) then   do something end

Should work.

Hope this help :slight_smile:

What you have to do is make sure is you buttons have a unique id, then you could test for phase(optional) and target.id.

Try this:

button1 = widget.newButton{ id = "button1", onEvent = btnRelease } button2 = widget.newButton{ id = "button2", onEvent = btnRelease }

local function btnRelease(event) if (event.phase == "ended" and event.target.id == "button1") then --do stuff end if (event.phase == "ended" and event.target.id == "button2") then --do stuff end end

Edit: Define the local function before this line of code: 

function scene:create(event)

Also, if you check for phase, an event listener is not needed.

Looks like i can’t get this to work

what is “function scene:create(event)” ? i don’t have it in my code

That is code from a Composer scene. If you’re not using Composer to manage multiple screens  you won’t have it.

Rob

This the composer scene code, I recommend you use it, it is very helpful for managing scenes.

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene