为什么我的 while 代码会导致模拟器崩溃?

每当我把 while 代码添加到我的游戏中,模拟器就停止响应了。我知道是 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
点赞
用户2653067
用户2653067

我不知道你为什么要不必要地使用 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
2015-03-17 07:52:56