我正在尝试编写类似于西蒙所说的游戏。我目前正在使用corona sdk作为我的平台。我已经写出了我的所有代码,但似乎当我启动它时,它不会运行我的算法并且不会产生随机模式。一旦我点击“点击开始”就应该运行算法,但没有任何反应。我尝试了各种方法,以及调试它,但似乎我找不到合适的解决方案。
我会发布所有代码,以防出现任何问题。
以下是代码:
-----------------------------------------------------------------------------------------
--
-- 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)
答案 0 :(得分:0)
我注意到的第一个错误是在onTouchListener函数中你要检查未初始化的值gameOver(这意味着它被设置为nil)并且你的条件将永远不会通过。 将此代码放入onTouchListener事件
print("pattern = "..pattern.." gameOver = "..gameOver)