timer.performWithDelay无法正常工作

时间:2013-06-06 06:41:10

标签: lua corona

我有以下代码:

local function RemovePlayer()
    print("Something")
end

function change(e)
    if(e.phase=="began")then
        Player.alpha=1
        Player.height=50
        Player.width=50
    end
    if(e.phase=="moved")then
    angle=(math.atan2( (e.y - Player.y), (e.x - Player.x))*180)/math.pi +90
    Player.rotation=angle
    end
    if(e.phase=="ended")then
        transition.to(Player,{time=200,height=32,width=32})
        local xx = (e.x-Player.x)*2
        local yy = (e.y-Player.y)*2
        Player.bodyType = "dynamic"
        Player:applyForce( xx, yy, Player.x, Player.y )
        timer.performWithDelay ( 10000,RemovePlayer() )
    end

return true
end

问题在于timer.performWithDelay似乎无法正常工作,因为" Something"在结束阶段之后立即打印在控制台中,而不是10000延迟。知道为什么会这样吗?

2 个答案:

答案 0 :(得分:7)

这可能是因为:

  1. 您的计时器将首先执行该功能;然后在10000毫秒过后再次执行它。因此,您可以即时获得输出。

    如果你想让用户等待10秒;使用os.sleep( 10 )

  2. 另一个可能的原因是您在声明计时器时调用该函数。变化:

    timer.performWithDelay ( 10000,RemovePlayer() )
    

    timer.performWithDelay ( 10000, RemovePlayer )  -- Notice no () here
    

答案 1 :(得分:1)

尝试按以下方式调用计时器:

timer.performWithDelay ( 10000,RemovePlayer,1 ) 
-- 1 is the number of times that the 'RemovePlayer' function is to be called.

继续编码.........:)