So I have some code - and I need an if statement to check something only once, and then go to a different function without the code checking again and again.
--Sorry about the whitespace - I used Outlaw and when I copied and pasted it --messed up my code. --I ran this below part twice (patternDot1-2) to get 2 dots: math.randomseed(os.time()) patternDot1 = math.random(9) print ("pattern dot 1 is " .. patternDot1) --\> print which dot patternDot1 is --find out which dot patternDot1 is and then blink that dot if patternDot1 == 1 then transition.to(dot1, {time = 200, alpha = 0.2, onComplete = glow1Remove}) patternDot1 = dot1 elseif patternDot1 == 2 then transition.to(dot2, {time = 200, alpha = 0.2, onComplete = glow1Remove}) patternDot1 = dot2 end --end of aforementioned "below part" :) function userDotCopy2() local function afterGlowRemove2() if userDot2 == patternDot2 then userSelectCorrect2 = true end print("2nd dot user selected is: " .. tostring(userSelectCorrect2)) userDotCopy3() end local function onTouch2(event) userDot2 = event.target function glowRemove2(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove2}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove2}) userSelectCorrect2 = false end dot1:addEventListener("tap", onTouch2) dot2:addEventListener("tap", onTouch2) end function userDotCopy1() local function afterGlowRemove1() if userDot1 == patternDot1 then userSelectCorrect1 = true end print("1st dot user selected is: " .. tostring(userSelectCorrect1)) userDotCopy2() --\> call userDot2 so it doesn't go again, but that isn't working end local function onTouch(event) userDot1 = event.target function glowRemove(obj) transition.to(obj, {time = 200, alpha = 1, onComplete = afterGlowRemove1}) end transition.to(event.target, {time = 150, alpha = 0.2, onComplete = glowRemove}) userSelectCorrect1 = false end dot1:addEventListener("tap", onTouch) dot2:addEventListener("tap", onTouch) end --start functions userDotCopy1() end
In my code, the computer sets two variables - patternDot1 and patternDot2.
Then, it waits for the user to click one of thedots. When one is clicked, the computer turns the alpha of that dot to 0.2 and then 1 again.
It then sets that dot (event.target) to userDot1, and compares userDot1 to patternDot1. Then it goes a second time.
The problem is, on the second time, it is still comparing userDot1 to patternDot1, and making it false since the first dot is, most of the time, different from the second. It isn’t a problem if the user selected the first one false, but what if it’s true? Then it needs to stop comparing. How can I do that?