Corona SDK中的While循环崩溃。

这段代码有什么问题,导致模拟器崩溃了吗? 我是 Corona SDK 的新手,但我在 Roblox 中了解了很多 Lua。

local x,y,touching,active = 2,2,false,true
local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,y)
background:scale(60,60)
print("Started")

function move()
    y=y+1
end

function onObjectTouch(event)
    if event.phase == "began" then
        touching = true
        while touching == true do
            timer.performWithDelay( 1000, move)
        end
    elseif event.phase == "ended" then
        touching = false
    end
    return true
end

background:addEventListener("touch",onObjectTouch)
点赞
用户2494064
用户2494064

Corona是基于事件的。你可以通过while循环锁定触摸事件,防止"ended"阶段触发,从而打破你的无限循环。

Corona有一个叫做"enterframe"的Runtime事件。它每秒被调用30-60次(可能根据它所做的工作不同而有所不同),它本质上是一个更新方法。

使用你的逻辑:

function onObjectTouch(event)
    if event.phase == "began" then
        touching = true
    elseif event.phase == "ended" then
        touching = false
    end
end

funtion EnterFrame(event)
    if touching == true then
            timer.performWithDelay( 1000, move)
    end
end

还有其他的方法可以实现这个,比如在触摸开始时设置动态对象的速度,在触摸结束时将速度设置为0。

2015-09-11 15:39:39