按住按钮达到2秒时,让角色奔跑。

我有一个角色,左右移动正确。现在我希望在按下按钮时,英雄能够奔跑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)

   -- 当没有按下箭头时停止角色移动
local function StopHero (event)
   if event.phase =="ended" then
      motionx = 0
   end
end
   Runtime:addEventListener("touch", StopHero )

有什么建议吗?

点赞
用户2767985
用户2767985

我想我找到答案了!

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
2014-08-26 11:34:11