我正在制作一个西蒙说用电晕sdk游戏,用Lua作为我的主要语言,我写了几个函数。当我运行我的代码时,它成功地完成了第一个循环,但是轮到我点击框后,在我点击“正确”框之后,程序就停止并给我一个游戏结束(在说明它是正确的之后)而不是再次循环并使用随机数向模式添加另一个模式索引...因此再次按以下顺序请求用户输入。
我已将初始值设置为:
started = false
pattern = true
gameOver = false
repeating = true
以下是我的代码的一部分: 启动功能:
function start()
--fix pat--
pat = {}
random = math.random(9)
patternIndex = 0
light = 2
clicked = 0
count = 0
end
等待功能:
function wait(seconds)
local _start = os.time()
local _end = _start+seconds
while (_end ~= os.time()) do
end
end
clickedNot功能:
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
在这里,我调用上面的函数:
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)
最后,这是使用'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)
请忽略用于调试问题的打印语句! 谢谢!
答案 0 :(得分:0)
Corona SDK是一个事件驱动的应用程序。你不应该以这种方式使用循环来使事情有效。按钮会生成事件然后让事情发生。人们触摸事物,然后你做更多的工作。
如果您需要传统的“游戏循环”,随着时间的推移会发生事情,您可以使用定时器以特定间隔触发,也可以创建一个“enterFrame”事件来触发每个屏幕更新。