Is it possible to meet multiple conditions to trigger an If statement?

Hello there!

I’m quite new to coding in general, but I’m trying to build a multiple-choice question game.

What I want to do currently is have the current selection deselected when another selection is made, or when the current selection is tapped again.

To do this, I’ve made two variables to keep track of the number of taps on a selection, and the last selection made.

I want both of these variables to be evaluated in an If/Then/Else statement.

This is the part of the code I’ve come up with to do this:

local function selectionOne() if (touchCount == 0) then touchCount = touchCount + 1 selectionIdentifier = "a" --make Selection One highlight elseif (touchCount \>= 1 and selectionIdentifier = "a") then --remove Selection One's highlight touchCount = 0 elseif (touchCount \>= 1 and selectionIdentifier = "b") then --remove Selection Two's highlight --make Selection One highlight touchCount = 0 end end

I’ve being using the ‘and’ operator to try and make the if/else statement do this, but I keep running into an error on Line 6  – ‘)’ expected near ‘=’

I assume this means that the program is being evaluated as if there’s an illegal statement that it’s ignoring.

So, is it possible for multiple conditions to have to be met in order to trigger an if/else/then statement? What am I doing wrong?

Thanks in advance.

==  “a”    not  = “a”    looks like just a typo, since you used the  ==  in the first if condition

so yes, it is possible to have multiple conditions be meet in order to trigger something.

Good luck!

There are a variety of ways to do what you are attempting.  Here is one way I have done similar code in the past.

Some methods might be better or cleaner, it al depends on what else your code is trying to do.  This is just on way to do what I think you are trying to do:

local widget = require("widget") W = display.contentWidth H = display.contentHeight MID\_W = display.contentWidth \* .5 MID\_H = display.contentHeight \* .5 local choices = { } local selectedButton = 0 -- 0 means no button selected local function onButton(e) if e.phase == "ended" then if selectedButton \> 0 then if e.target.id == selectedButton then e.target.alpha = .5 selectedButton = 0 else -- deselect them all first for i = 1, #choices do choices[i].alpha = .5 end selectedButton = e.target.id e.target.alpha = 1 end else selectedButton = e.target.id e.target.alpha = 1 end end end for i = 1, 4 do local btn = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", onEvent = onButton } btn.x = MID\_W btn.y = 50 + (i \* 50) btn.alpha = .5 -- assign a identifier to the button btn.id = i table.insert(choices, btn) end

Good Luck.

Bob

Thank you cyberparkstudios for the answers.

I was able to solve the issue shortly after I had sent in the thread for moderation - and, yeah, it was that typo.

It took a lot of working and additional text, but I did manage to get that method to work in the end. I know there’s more efficient ways to do it, like the method you provided (thanks again for that, by the way), but I just don’t understand them very much at all. I’ll work around this one for now, and then redefine it at a later stage.

Here’s the final code I used, albeit, with removed extras:

local function selectionOne() if (touchCount == 0) then --on initial tap multiOneBox:setFillColor(0.39, 0.63, 1) --highlight selection 1 selectionIdentifier = "a" --make question 1 the last selected touchCount = 1 --set touchCount to one elseif (touchCount \>= 1 and selectionIdentifier == "b") then --on tap from selection 2 multiTwoBox:setFillColor(1, 1, 1) --remove highlight selection 2 multiOneBox:setFillColor(0.39, 0.63, 1) --highlight selection 1 selectionIdentifier = "a" --make question 1 the last selected touchCount = 1 --set touchCount to one else --on second tap selection 1         multiOneBox:setFillColor(1, 1, 1) --remove highlight selection 1         touchCount = 0 --set touchCount to zero end end local function selectionTwo() if (touchCount == 0) then --on initial tap multiTwoBox:setFillColor(0.39, 0.63, 1) --highlight selection 2 selectionIdentifier = "b" --make question 2 the last selected touchCount = 1 --set touchCount to one elseif (touchCount \>= 1 and selectionIdentifier == "a") then --on tap from selection 1 multiOneBox:setFillColor(1, 1, 1) --remove highlight selection 1 multiTwoBox:setFillColor(0.39, 0.63, 1) --highlight selection 2 selectionIdentifier = "b" --make question 2 the last selected touchCount = 1 --set touchCount to one else --on second tap selection 2          multiTwoBox:setFillColor(1, 1, 1) --remove highlight selection 2          touchCount = 0 --set touchCount to zero end end

==  “a”    not  = “a”    looks like just a typo, since you used the  ==  in the first if condition

so yes, it is possible to have multiple conditions be meet in order to trigger something.

Good luck!

There are a variety of ways to do what you are attempting.  Here is one way I have done similar code in the past.

Some methods might be better or cleaner, it al depends on what else your code is trying to do.  This is just on way to do what I think you are trying to do:

local widget = require("widget") W = display.contentWidth H = display.contentHeight MID\_W = display.contentWidth \* .5 MID\_H = display.contentHeight \* .5 local choices = { } local selectedButton = 0 -- 0 means no button selected local function onButton(e) if e.phase == "ended" then if selectedButton \> 0 then if e.target.id == selectedButton then e.target.alpha = .5 selectedButton = 0 else -- deselect them all first for i = 1, #choices do choices[i].alpha = .5 end selectedButton = e.target.id e.target.alpha = 1 end else selectedButton = e.target.id e.target.alpha = 1 end end end for i = 1, 4 do local btn = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", onEvent = onButton } btn.x = MID\_W btn.y = 50 + (i \* 50) btn.alpha = .5 -- assign a identifier to the button btn.id = i table.insert(choices, btn) end

Good Luck.

Bob

Thank you cyberparkstudios for the answers.

I was able to solve the issue shortly after I had sent in the thread for moderation - and, yeah, it was that typo.

It took a lot of working and additional text, but I did manage to get that method to work in the end. I know there’s more efficient ways to do it, like the method you provided (thanks again for that, by the way), but I just don’t understand them very much at all. I’ll work around this one for now, and then redefine it at a later stage.

Here’s the final code I used, albeit, with removed extras:

local function selectionOne() if (touchCount == 0) then --on initial tap multiOneBox:setFillColor(0.39, 0.63, 1) --highlight selection 1 selectionIdentifier = "a" --make question 1 the last selected touchCount = 1 --set touchCount to one elseif (touchCount \>= 1 and selectionIdentifier == "b") then --on tap from selection 2 multiTwoBox:setFillColor(1, 1, 1) --remove highlight selection 2 multiOneBox:setFillColor(0.39, 0.63, 1) --highlight selection 1 selectionIdentifier = "a" --make question 1 the last selected touchCount = 1 --set touchCount to one else --on second tap selection 1         multiOneBox:setFillColor(1, 1, 1) --remove highlight selection 1         touchCount = 0 --set touchCount to zero end end local function selectionTwo() if (touchCount == 0) then --on initial tap multiTwoBox:setFillColor(0.39, 0.63, 1) --highlight selection 2 selectionIdentifier = "b" --make question 2 the last selected touchCount = 1 --set touchCount to one elseif (touchCount \>= 1 and selectionIdentifier == "a") then --on tap from selection 1 multiOneBox:setFillColor(1, 1, 1) --remove highlight selection 1 multiTwoBox:setFillColor(0.39, 0.63, 1) --highlight selection 2 selectionIdentifier = "b" --make question 2 the last selected touchCount = 1 --set touchCount to one else --on second tap selection 2          multiTwoBox:setFillColor(1, 1, 1) --remove highlight selection 2          touchCount = 0 --set touchCount to zero end end