CheckBox conditions met before going to the next scene

I have a questions to which I was having trouble finding the correct documentation. 

I’d like to have 3 checkboxes “checked” before the player is allowed to goto the next scene. 

I currently have an addEventListener function on a button to goto the next scene.

I don’t mind changing the mechanics as long as the condition of 3 checkboxes (there are more than 3) are true before changing scenes is allowed.

Maybe someone can point me in the right direction or if you know the answer :) 

I’m still pretty new at this, thanks in advance.

Hi @richtornado,

Widget switches of “checkbox” type should do what you need:

https://docs.coronalabs.com/api/library/widget/newSwitch.html

You’ll just need to loop through and check all checkboxes for the “.isOn” property (of “true”) before you let the user continue:

https://docs.coronalabs.com/api/type/SwitchWidget/isOn.html

Hope this helps,

Brent

Wow, thank you Brent. I was actually just reading the same link. Still trying to make heads or tails of what the code looks like.

In my case I have several checkBoxes and will need the user to have a minimum of 3 of them checked before being allowed to goto the next scene.

I’m not sure how to make “3 true” = gotoScene"

Ah, that should be easy enough… just keep a counter variable, and in the switch listener (you can use one for all switches), just increase the counter each time you detect “.isOn == true”, and decrease it by 1 each time you detect “.isOn == false”.

Thank you once again. I’m just not sure what that looks like code-wise.

Sorry I’m still very new to this. 

This would be the counter variable (I’ve been Googling for “counter variable”

local score = 0

for adding the score I tried this but I know it’s wrong.

local function onSwitchPress( event ) local switch = event.target print( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) ) score + 1 end

Check out the example code at the bottom:

https://docs.coronalabs.com/api/library/widget/newSwitch.html#checkboxes

That shows how to use a switch listener for checkboxes, and how to read the “.isOn” property. Based on whether it’s true or false, just increase or decrease some variable which is scoped outside of the listener function. Once it’s 3 or higher, the user can proceed.

Brent

This basically should do it…

[lua]

local function onSwitchPress( event )

    local switch = event.target

    if switch.isOn == true then

        score = score + 1

    elseif switch.isOn == false then

        score = score - 1

    end

end

[/lua]

Cool, i actually get that part, nice. Thank you for the great example.

I’m now on the last leg of my app. I’m just trying to type out the conditioner to allow a score of 3 to go on to the next screen but it’s given me errors.

local function increaseScore(event) if (score = 3) then changeScene3() end return true end

I fixed it!

Changed the 

 if (score = 3) then changeScene3()

to

 if (score \> 2) then changeScene3()

and it worked!

Now I just have to "reset them to “off” when the next scene loads.

Thank you once again @Brent for getting me on the right track.

Hi @richtornado,

Widget switches of “checkbox” type should do what you need:

https://docs.coronalabs.com/api/library/widget/newSwitch.html

You’ll just need to loop through and check all checkboxes for the “.isOn” property (of “true”) before you let the user continue:

https://docs.coronalabs.com/api/type/SwitchWidget/isOn.html

Hope this helps,

Brent

Wow, thank you Brent. I was actually just reading the same link. Still trying to make heads or tails of what the code looks like.

In my case I have several checkBoxes and will need the user to have a minimum of 3 of them checked before being allowed to goto the next scene.

I’m not sure how to make “3 true” = gotoScene"

Ah, that should be easy enough… just keep a counter variable, and in the switch listener (you can use one for all switches), just increase the counter each time you detect “.isOn == true”, and decrease it by 1 each time you detect “.isOn == false”.

Thank you once again. I’m just not sure what that looks like code-wise.

Sorry I’m still very new to this. 

This would be the counter variable (I’ve been Googling for “counter variable”

local score = 0

for adding the score I tried this but I know it’s wrong.

local function onSwitchPress( event ) local switch = event.target print( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) ) score + 1 end

Check out the example code at the bottom:

https://docs.coronalabs.com/api/library/widget/newSwitch.html#checkboxes

That shows how to use a switch listener for checkboxes, and how to read the “.isOn” property. Based on whether it’s true or false, just increase or decrease some variable which is scoped outside of the listener function. Once it’s 3 or higher, the user can proceed.

Brent

This basically should do it…

[lua]

local function onSwitchPress( event )

    local switch = event.target

    if switch.isOn == true then

        score = score + 1

    elseif switch.isOn == false then

        score = score - 1

    end

end

[/lua]

Cool, i actually get that part, nice. Thank you for the great example.

I’m now on the last leg of my app. I’m just trying to type out the conditioner to allow a score of 3 to go on to the next screen but it’s given me errors.

local function increaseScore(event) if (score = 3) then changeScene3() end return true end

I fixed it!

Changed the 

 if (score = 3) then changeScene3()

to

 if (score \> 2) then changeScene3()

and it worked!

Now I just have to "reset them to “off” when the next scene loads.

Thank you once again @Brent for getting me on the right track.