While loop troubles

This code crashes my game, any idea why?

local x,y,touching,active = 2,2,false,true
local background = display.newImage(“Icon.png”,(display.pixelWidth/2)+30,(display.pixelHeight/2)+30)
background:scale(60,60)
print(“Started”)
function onObjectTouch(event)
    if event.phase == “began” then
        touching = true
    elseif event.phase == “ended” then
        touching = false
    end
    return true
end

background:addEventListener(“touch”,onObjectTouch)

while active == true do
    if touching == true then
        y = y + 1
    elseif touching == false then
        y = y - 1
    end
end

I never see where you are changing the value of “active”. If it starts true, it stays true and the look continues executing in a very tight pattern which will give you feeling that you’re locked up.

Rob

  1. Please format your future code questions w/ code block.

formatyourcode.jpg

  1. Corona is not multi-threaded and that loop will never end, so it will never check for a collision.

This would work (not a great way to do it, but it will work)

local x,y,touching,active = 2,2,false,true local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,(display.pixelHeight/2)+30) background:scale(60,60) print("Started") function onObjectTouch(event) if event.phase == "began" then touching = true elseif event.phase == "ended" then touching = false end return true end background:addEventListener("touch",onObjectTouch) local function onEnterFrame() if touching == true then y = y + 1 elseif touching == false then y = y - 1 end end Runtime:addEventListener( "enterFrame", onEnterFrame )

I never see where you are changing the value of “active”. If it starts true, it stays true and the look continues executing in a very tight pattern which will give you feeling that you’re locked up.

Rob

  1. Please format your future code questions w/ code block.

formatyourcode.jpg

  1. Corona is not multi-threaded and that loop will never end, so it will never check for a collision.

This would work (not a great way to do it, but it will work)

local x,y,touching,active = 2,2,false,true local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,(display.pixelHeight/2)+30) background:scale(60,60) print("Started") function onObjectTouch(event) if event.phase == "began" then touching = true elseif event.phase == "ended" then touching = false end return true end background:addEventListener("touch",onObjectTouch) local function onEnterFrame() if touching == true then y = y + 1 elseif touching == false then y = y - 1 end end Runtime:addEventListener( "enterFrame", onEnterFrame )