I am making a simon says game with corona sdk, using Lua as my main language, and I have wrote several functions. When I run my code, it successfully goes through the first loop, but after it's my turn to click the box, and after I click the CORRECT box, the program just stops and gives me a game over (after stating that it was correct), instead of looping over again and adding another pattern index to the pattern with a random number... thus asking for the user's input in the following order again.
I have set the initial values to these:
started = false
pattern = true
gameOver = false
repeating = true
Here are part of my code: Start function:
function start()
--fix pat--
pat = {}
random = math.random(9)
patternIndex = 0
light = 2
clicked = 0
count = 0
end
wait function:
function wait(seconds)
local _start = os.time()
local _end = _start+seconds
while (_end ~= os.time()) do
end
end
clickedNot function:
function clickedNot()
if(clicked ~= 0) then
--if ur right--
if(pat[patternIndex] == clicked) then
print("ur right!")
print("patternIndex: "..patternIndex)
patternIndex = patternIndex + 1
print("patternIndex after ++: "..patternIndex)
print(table.getn(pat))
print("light: "..light)
repeating = true
end
if(pat[patternIndex] ~= clicked) then
gameOver = true
gameoverText = display.newText("Game Over!", 0, 0, native.systemFont, 40)
gameoverText.x = display.contentWidth/2
gameoverText.y = display.contentWidth/4
gameoverText:setTextColor(255,110,110)
print("Game Over")
end
end
end
Here, I call the function above:
function onTouchListener(event)
if (pattern == false and gameOver == false) then
if(event.target == btnclick1) then
count = 1
clicked = 1
elseif(event.target == btnclick2) then
count = 1
clicked = 2
elseif(event.target == btnclick3) then
count = 1
clicked = 3
elseif(event.target == btnclick4) then
count = 1
clicked = 4
elseif(event.target == btnclick5) then
count = 1
clicked = 5
elseif(event.target == btnclick6) then
count = 1
clicked = 6
elseif(event.target == btnclick7) then
count = 1
clicked = 7
elseif(event.target == btnclick8) then
count = 1
clicked = 8
elseif(event.target == btnclick9) then
count = 1
clicked = 9
end
paint()
clickedNot()
elseif (gameOver == true) then
start()
gameOver = false
end
end
btnclick1: addEventListener( "touch", onTouchListener)
btnclick2: addEventListener( "touch", onTouchListener)
btnclick3: addEventListener( "touch", onTouchListener)
btnclick4: addEventListener( "touch", onTouchListener)
btnclick5: addEventListener( "touch", onTouchListener)
btnclick6: addEventListener( "touch", onTouchListener)
btnclick7: addEventListener( "touch", onTouchListener)
btnclick8: addEventListener( "touch", onTouchListener)
btnclick9: addEventListener( "touch", onTouchListener)
Finally, here is the main function with the algorithm of 'looping'
function starting(event)
if (event.phase == "began") then
startText.isVisible = false
if (started == false) then
started = true
end
start()
count = count + 1
onTouchListener(event)
---------------------------------
print("count: "..count)
while (repeating == true) do
print("repeatedddd")
if (started == true) then
print("hello")
--if started--
----------------------------------------------
if(count%20 == count - math.floor(count/20)*20) then
clicked = 0
while(light >= 0) do
light = light - 1
print(light)
end
end
if (pattern == true) then
if (light <= 0) then
wait(1)
if (patternIndex >= table.getn(pat)) then
--randomizes lights--
clicked = math.random(0,8)+1
table.insert(pat,clicked)
patternIndex = 1
pattern = false
print ("box: "..clicked)
print("element 1 in pattern: "..pat[1])
wait(1)
print("your turn")
print("size of array: "..table.getn(pat))
repeating = false
else
clicked = pat[patternIndex]
patternIndex = patternIndex + 1
print("lights up random box")
print (clicked)
wait(1)
end
light = 1
end
elseif (patternIndex == table.getn(pat)) then
pattern = true
patternIndex = 0
light = 2
wait(1)
print("your turn ended")
print(pat[patternIndex])
end
end
end
end
end
startText: addEventListener( "touch", starting)
Please ignore the print statements as it was used to debug the problem! Thanks!
pat. You initialize the table withpat = { }, but without setting pat[0] or pat[1] to something, then the comparisonif(pat[patternIndex] ~= clicked) thenwill always be true. - Mike Andrews