Reputation: 35
Whenever I add the while code to my game, the simulator stops responding. I know its the while code that causes this problem because if I take the while code out, the game works like its supposed to. Whats wrong with my while code, and how do i fix it? Let me know if you need any more information to solve the problem. Here is the code:
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
Upvotes: 1
Views: 163
Reputation: 521
I dont know why you are using while and the if statement unnecessarily, try this it should work.
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
Upvotes: 4