Hi!
I am trying to code a game similar to simon says. I am currently using corona sdk as my platform. I have written out all my code, but it seems that when I start it, it does not run through my algorithm and does not make a random pattern. It is supposed to run the algorithm once I click on ‘Click to Start’, but nothing happens. I tried various ways, as well as debugging it, but it seems that I can’t find the right solution to this! :[
I would greatly appreciate it if you guys please lead me to the right direction and tell me what I am doing wrong!
Ill post all of my code just in case anything is wrong!
Heres the code:
–
– main.lua
–
count = 0
pat = {1,2,3,4,5,6,7,8,9}
–>Adds background image
local background = display.newImage(“wallpaper.jpg”)
local startText = display.newText(“Click to Start!”, 0, 0, native.systemFont, 40)
startText.x = display.contentWidth/2
startText.y = display.contentWidth/4
startText:setTextColor(255,110,110)
–>Adds Boxes - row 1
local box1 = display.newImage(“my_box.png”)
box1.x = display.contentWidth/5
box1.y = display.contentWidth/2
local btnclick1 = display.newImage(“clickedbox1.png”)
btnclick1.x = display.contentWidth/5
btnclick1.y = display.contentHeight/3
local box2 = display.newImage(“my_box.png”)
box2.x = display.contentWidth/2
box2.y = display.contentWidth/2
local btnclick2 = display.newImage(“clickedbox1.png”)
btnclick2.x = display.contentWidth/2
btnclick2.y = display.contentHeight/3
local box3 = display.newImage(“my_box.png”)
box3.x = display.contentWidth/1.25
box3.y = display.contentWidth/2
local btnclick3 = display.newImage(“clickedbox1.png”)
btnclick3.x = display.contentWidth/1.25
btnclick3.y = display.contentHeight/3
–>Adds Boxes - row 2
local box4 = display.newImage(“my_box.png”)
box4.x = display.contentWidth/5
box4.y = display.contentWidth/1.25
local btnclick4 = display.newImage(“clickedbox1.png”)
btnclick4.x = display.contentWidth/5
btnclick4.y = display.contentHeight/1.875
local box5 = display.newImage(“my_box.png”)
box5.x = display.contentWidth/2
box5.y = display.contentWidth/1.25
local btnclick5 = display.newImage(“clickedbox1.png”)
btnclick5.x = display.contentWidth/2
btnclick5.y = display.contentHeight/1.875
local box6 = display.newImage(“my_box.png”)
box6.x = display.contentWidth/1.25
box6.y = display.contentWidth/1.25
local btnclick6 = display.newImage(“clickedbox1.png”)
btnclick6.x = display.contentWidth/1.25
btnclick6.y = display.contentHeight/1.875
–>Adds Boxes - row 3
local box7 = display.newImage(“my_box.png”)
box7.x = display.contentWidth/5
box7.y = display.contentWidth/.91
local btnclick7 = display.newImage(“clickedbox1.png”)
btnclick7.x = display.contentWidth/5
btnclick7.y = display.contentHeight/1.365
local box8 = display.newImage(“my_box.png”)
box8.x = display.contentWidth/2
box8.y = display.contentWidth/.91
local btnclick8 = display.newImage(“clickedbox1.png”)
btnclick8.x = display.contentWidth/2
btnclick8.y = display.contentHeight/1.365
local box9 = display.newImage(“my_box.png”)
box9.x = display.contentWidth/1.25
box9.y = display.contentWidth/.91
local btnclick9 = display.newImage(“clickedbox1.png”)
btnclick9.x = display.contentWidth/1.25
btnclick9.y = display.contentHeight/1.365
–if the box is clicked, it lights up (by displaying another image over it)–
local onTouchListener = function(event)
if(event.phase == “began”) then
if (pattern == false and gameOver == false) then
if(event.target == btnclick1) then
clicked = 1
count = 1
btnclicked1 = display.newImage(“clickedbox.png”)
btnclicked1.x = display.contentWidth/5
btnclicked1.y = display.contentHeight/3
elseif(event.target == btnclick2) then
clicked = 2
count = 1
btnclicked2 = display.newImage(“clickedbox.png”)
btnclicked2.x = display.contentWidth/2
btnclicked2.y = display.contentHeight/3
elseif(event.target == btnclick3) then
clicked = 3
count = 1
btnclicked3 = display.newImage(“clickedbox.png”)
btnclicked3.x = display.contentWidth/1.25
btnclicked3.y = display.contentHeight/3
elseif(event.target == btnclick4) then
clicked = 4
count = 1
btnclicked4 = display.newImage(“clickedbox.png”)
btnclicked4.x = display.contentWidth/5
btnclicked4.y = display.contentHeight/1.875
elseif(event.target == btnclick5) then
clicked = 5
count = 1
btnclicked5 = display.newImage(“clickedbox.png”)
btnclicked5.x = display.contentWidth/2
btnclicked5.y = display.contentHeight/1.875
elseif(event.target == btnclick6) then
clicked = 6
count = 1
btnclicked6 = display.newImage(“clickedbox.png”)
btnclicked6.x = display.contentWidth/1.25
btnclicked6.y = display.contentHeight/1.875
elseif(event.target == btnclick7) then
clicked = 7
count = 1
btnclicked7 = display.newImage(“clickedbox.png”)
btnclicked7.x = display.contentWidth/5
btnclicked7.y = display.contentHeight/1.365
elseif(event.target == btnclick8) then
clicked = 8
count = 1
btnclicked8 = display.newImage(“clickedbox.png”)
btnclicked8.x = display.contentWidth/2
btnclicked8.y = display.contentHeight/1.365
elseif(event.target == btnclick9) then
clicked = 9
count = 1
btnclicked9 = display.newImage(“clickedbox.png”)
btnclicked9.x = display.contentWidth/1.25
btnclicked9.y = display.contentHeight/1.365
end
if(clicked ~= 0) then
if(pat[patternIndex] == clicked) then
patternIndex = patternIndex + 1
else
gameOver = true
end
end
elseif (gameOver) then
started()
gameOver = false
end
elseif(event.phase == “ended”) then
if (btnclicked1 ~= nil) then
btnclicked1:removeSelf()
btnclicked1 = nil
elseif(btnclicked2 ~= nil) then
btnclicked2:removeSelf()
btnclicked2 = nil
elseif(btnclicked3 ~= nil) then
btnclicked3:removeSelf()
btnclicked3 = nil
elseif(btnclicked4 ~= nil) then
btnclicked4:removeSelf()
btnclicked4 = nil
elseif(btnclicked5 ~= nil) then
btnclicked5:removeSelf()
btnclicked5 = nil
elseif(btnclicked6 ~= nil) then
btnclicked6:removeSelf()
btnclicked6 = nil
elseif(btnclicked7 ~= nil) then
btnclicked7:removeSelf()
btnclicked7 = nil
elseif(btnclicked8 ~= nil) then
btnclicked8:removeSelf()
btnclicked8 = nil
elseif(btnclicked9 ~= nil) then
btnclicked9:removeSelf()
btnclicked9 = nil
end
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)
–if started, starts algorithm–
local started = function(event)
if (event.phase == “ended”) then
pat = {1, 2, 3, 4, 5, 6, 7, 8, 9}
random = math.random()
patternIndex = 0
light = 2
clicked = 0
count = count + 1
pattern = true
if (startText ~= nil) then
startText:removeSelf()
startText = nil
end
--if started–
if(math.mod(count,20) == 0) then
clicked = 0
if(light >= 0) then
light = light - 1
end
end
if (pattern) then
if (light <= 0) then
if (patternIndex >= table.getn(pat)) then
--randomizes lights–
clicked = math.random(1,10)+1
table.insert(pat,clicked)
patternIndex = 0
pattern = false
else
clicked = pat[patternIndex]
patternIndex = patternIndex + 1
end
light = 1
end
elseif (patternIndex == table.getn(pat)) then
pattern = true
patternIndex = 0
light = 2
end
end
end
startText: addEventListener( “touch”, started)
Thank you!