How to determine which specific button was pressed out of multiple buttons?

I want to give the user the ability to choose a character out of a choice of two. How can I determine which button was chosen? Could I check for a specific id? Furthermore, can composer.set and composer.get be used to set the character in the next scene? 

Thank you.

Sincerely,

Alex

--1. and 2. local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print(event.target.id) if event.target.\_id == "button1" then print("button 1 was pressed) elseif event.target.id == "button2" then print("button 2 was pressed") end end end -- Create the widget local button1 = widget.newButton( { left = 100, top = 100, id = "button1", label = "Default", onEvent = handleButtonEvent } ) local button2 = widget.newButton( { left = 100, top = 300, id = "button2", label = "Default", onEvent = handleButtonEvent } )
--scene one local json = require("json") local composer = require("composer) local player = {name = "bob", height = 100} composer.setVariable("player", json.encode(player)) --scene two local json = require("json") local composer = require("composer) local player = json.decode(composer.getVariable( "player" ))

you can try something like this :  btn1 has an id of ‘1’,   btn2 has an id of ‘2’

local widget = require( "widget" ) local choice local function onButton( event ) local target = event.target if event.phase == "ended" then if target.id == 1 then choice = "MICKEY" else choice = "DONALD" end print(" choice is " .. choice) end end local btn1 = widget.newButton { defaultFile="Textures/Buttons/button.png", overFile="Textures/Buttons/button\_Dn.png", width = 100, height = 30, id = 1, label = "MICKEY", onEvent = onButton } btn1.x = 100 btn1.y = 50 local btn2 = widget.newButton { defaultFile="Textures/Buttons/button.png", overFile="Textures/Buttons/button\_Dn.png", width = 100, height = 30, id = 2, label = "DONALD", onEvent = onButton } btn2.x = 200 btn2.y = 50

as far as get and set, check out this link to the corona documentation for composer.setVariable and composer.getVariable … it explains it fairly well 

https://docs.coronalabs.com/api/library/composer/setVariable.html

good luck

Bob

sorry about that,… did not know scottRules44 already answered your question.

it is ok

Thank you both, but I figured it out on my own:

local function onPlayBtnRelease(event) if (event.phase == "ended" and event.target.id == "girl") then print(event.target.id) end if (event.phase == "ended" and event.target.id == "boy") then print(event.target.id) end end

But thanks, anyway. :slight_smile:

--1. and 2. local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print(event.target.id) if event.target.\_id == "button1" then print("button 1 was pressed) elseif event.target.id == "button2" then print("button 2 was pressed") end end end -- Create the widget local button1 = widget.newButton( { left = 100, top = 100, id = "button1", label = "Default", onEvent = handleButtonEvent } ) local button2 = widget.newButton( { left = 100, top = 300, id = "button2", label = "Default", onEvent = handleButtonEvent } )
--scene one local json = require("json") local composer = require("composer) local player = {name = "bob", height = 100} composer.setVariable("player", json.encode(player)) --scene two local json = require("json") local composer = require("composer) local player = json.decode(composer.getVariable( "player" ))

you can try something like this :  btn1 has an id of ‘1’,   btn2 has an id of ‘2’

local widget = require( "widget" ) local choice local function onButton( event ) local target = event.target if event.phase == "ended" then if target.id == 1 then choice = "MICKEY" else choice = "DONALD" end print(" choice is " .. choice) end end local btn1 = widget.newButton { defaultFile="Textures/Buttons/button.png", overFile="Textures/Buttons/button\_Dn.png", width = 100, height = 30, id = 1, label = "MICKEY", onEvent = onButton } btn1.x = 100 btn1.y = 50 local btn2 = widget.newButton { defaultFile="Textures/Buttons/button.png", overFile="Textures/Buttons/button\_Dn.png", width = 100, height = 30, id = 2, label = "DONALD", onEvent = onButton } btn2.x = 200 btn2.y = 50

as far as get and set, check out this link to the corona documentation for composer.setVariable and composer.getVariable … it explains it fairly well 

https://docs.coronalabs.com/api/library/composer/setVariable.html

good luck

Bob

sorry about that,… did not know scottRules44 already answered your question.

it is ok

Thank you both, but I figured it out on my own:

local function onPlayBtnRelease(event) if (event.phase == "ended" and event.target.id == "girl") then print(event.target.id) end if (event.phase == "ended" and event.target.id == "boy") then print(event.target.id) end end

But thanks, anyway. :slight_smile: