如何不断检查lua中对象的位置

时间:2015-03-07 21:01:44

标签: lua corona

我正在制作一个游戏,该游戏涉及绘制线条以将物理球引导到屏幕底部的桶中。但是,有时用户可能会画线很差,并且球会卡住。为了摆脱这种情况,我想每隔3秒检查一次球的位置。

这是我以为的工作原理:

 function checkBallVelocity(event)
    startX = event.x
    startY = event.y

    timer.performWithDelay( 5000, function()

        endX = event.x
        endY = event.y

        print(startX..","..startY.." || "..endX..","..endY)
        if startX == endX or startY == endY then

            if event.other.name == "1" then

                circle1:removeSelf( )
                circle1 = nil

                ballsMissed = ballsMissed + 1
            elseif event.other.name == "2" then

                circle2:removeSelf( )
                circle2 = nil

                ballsMissed = ballsMissed + 1
            elseif event.other.name == "3" then

                circle3:removeSelf( )
                circle3 = nil

                ballsMissed = ballsMissed + 1
            end
            return 1
        else
            checkBallVelocity()
        end
    end
    )
end

可悲的是,它没有。欢迎任何帮助/建议

1 个答案:

答案 0 :(得分:0)

有很多方法可以做这种事情,但是Corona提供了一个enterFrame事件,每次绘制一个帧时都会触发(每秒30或60帧)。它可以用作执行定期操作的主循环,但请记住它经常完全运行,因此它有助于定义句点并将您的操作限制在该时段内的某些阶段。

local period
local phase = 0
function mainLoop(event) 
    phase = phase + 1
    if phase == 1 then 
       checkBallPosition(...)
    elseif phase == period
       phase = 0
    end
end
...
function scene:enterScene( event ) 
    period = display.fps * 3 -- # of seconds
    Runtime:addEventListener("enterFrame", mainLoop)
    ...
end
相关问题