【问题标题】:Why does my while code cause my simulator to crash?为什么我的 while 代码会导致我的模拟器崩溃?
【发布时间】:2015-03-17 15:52:56
【问题描述】:

每当我将 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

【问题讨论】:

    标签: while-loop lua coronasdk


    【解决方案1】:

    我不知道你为什么不必要地使用 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
    

    【讨论】:

    • 谢谢库马尔!我之所以如此复杂,是因为我试图根据顶部出现的随机生成的图像将两个函数循环在一起......关于如何做到这一点的任何想法?
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多