timer.performWithDelay无法正常工作

Avr*_*dis 1 lua coronasdk

我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

问题是timer.performWithDelay看起来似乎不正确,因为"Something"在结束阶段之后立即打印在控制台中而不是10000延迟.知道为什么会这样吗?

hjp*_*r92 7

发生这种情况可能是因为:

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

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

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

    timer.performWithDelay ( 10000,RemovePlayer() )
    
    Run Code Online (Sandbox Code Playgroud)

    timer.performWithDelay ( 10000, RemovePlayer )  -- Notice no () here
    
    Run Code Online (Sandbox Code Playgroud)