lua - Why does my while code cause my simulator to crash? -
whenever add while code game, simulator stops responding. know while code causes problem because if take while code out, game works supposed to. whats wrong while code, , how fix it? let me know if need more information solve problem. here 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" 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" if imagefile ~= "redbox.png" storyboard.gotoscene( "restarteasy" ) end end end end local function endgame2(event) if imagefile == "bluebox.png" 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" if imagefile ~= "bluebox.png" storyboard.gotoscene("restarteasy") end end end end button1:addeventlistener("tap", endgame) button2:addeventlistener("tap", endgame2) end
i dont know why using while , if statement unnecessarily, try should work.
local function endgame(event) if imagefile == "redbox.png" 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
Comments
Post a Comment