每当我将while代码添加到我的游戏中时,模拟器就会停止响应。我知道它导致这个问题的while代码,因为如果我把while代码拿出来,游戏就像它应该的那样工作。我的代码有什么问题,我该如何解决?如果您需要更多信息来解决问题,请与我们联系。这是代码:
function scene:createScene ( event )
local group = self.view
local tap = display.newText("Tap:", 0, 0, "Helvetica", 36)
tap.x = 100
tap.y = screenTop + 20
group:insert(tap)
local imageFiles = {"redbox.png", "bluebox.png"}
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
local button1 = display.newImage("redbox.png")
button1.x = centerX
button1.y = centerY
group:insert(button1)
local button2 = display.newImage("bluebox.png")
button2.x = centerX
button2.y = centerY - 100
group:insert(button2)
local function endGame(event)
if imageFile == "redbox.png" then
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
while imageFile == "redbox.png" do
if imageFile ~= "redbox.png" then
storyboard.gotoScene( "restartEasy" )
end
end
end
end
local function endGame2(event)
if imageFile == "bluebox.png" then
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
while imageFile == "bluebox.png" do
if imageFile ~= "bluebox.png" then
storyboard.gotoScene("restartEasy")
end
end
end
end
button1:addEventListener("tap", endGame)
button2:addEventListener("tap", endGame2)
end
答案 0 :(得分:4)
我不知道你为什么使用while和if语句不必要,试试这应该工作。
local function endGame(event)
if imageFile == "redbox.png" then
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, centerX, screenTop + 20)
else
storyboard.gotoScene( "restartEasy" )
end
end