在按住按钮的同时使角色运行2秒

时间:2014-08-26 09:56:28

标签: lua corona

我有一个正确左右移动的角色。我想现在英雄在2秒内按下按钮时运行。

这是我的实际代码:

function LeftArrow(event)   
    motionx = -hero.speed   
end

function RightArrow(event)
    motionx = hero.speed
end

local function MoveHero (event)
     hero.x = hero.x + motionx

end
Runtime:addEventListener("enterFrame", MoveHero)

    -- Stop character movement when no arrow is pushed
local function StopHero (event)
    if event.phase =="ended" then
       motionx = 0
    end
end
    Runtime:addEventListener("touch", StopHero )

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了答案!

function GoFastLeft()
hero.speed=10
motionx = -hero.speed
end

function GoFastRight()
hero.speed=10
motionx = hero.speed
end


function LeftArrow(event)
   if ( event.phase == "began" ) then
        motionx = -hero.speed
        FastTimer = timer.performWithDelay( 1000, GoFastLeft, 1 )
   elseif ( event.phase == "ended" ) then
      hero.speed=2
      timer.cancel( FastTimer )
      motionx = 0
   end
   return true  

end

function RightArrow(event)
      if ( event.phase == "began" ) then
        motionx = hero.speed
        FastTimer2 = timer.performWithDelay( 1000, GoFastRight, 1 )
   elseif ( event.phase == "ended" ) then
      hero.speed=2
      timer.cancel( FastTimer2 )
      motionx = 0
   end
   return true  
end