使用Lua移动角色

使用Lua移动角色,lua,coronasdk,Lua,Coronasdk,我是Lua的新手,正在尝试模拟角色移动。 我现在让角色左右移动。我希望角色一次移动16个像素。考虑到用户不会快速触摸手机,这样做很好。在这种情况下,角色将移动随机数目的像素 我的问题是,如何让触摸事件一次只注册一次 我的代码: -- move character function moveCharacter(event) if event.phase == 'began' then if event.x > character.x+8 then

我是Lua的新手,正在尝试模拟角色移动。 我现在让角色左右移动。我希望角色一次移动16个像素。考虑到用户不会快速触摸手机,这样做很好。在这种情况下,角色将移动随机数目的像素

我的问题是,如何让触摸事件一次只注册一次

我的代码:

-- move character
function moveCharacter(event)
    if  event.phase == 'began' then
        if event.x > character.x+8 then
            transition.to(background, {time=800, x=background.x-16})
        end

        if event.x < character.x-8 then
            transition.to(background, {time=800, x=background.x+16})
        end
    end
end

function touchScreen(event)
    Runtime:removeEventListener('touch', moveCharacter)

    if  event.phase == 'began' then
        Runtime:addEventListener('touch', moveCharacter)
    end

end

Runtime:addEventListener('touch', touchScreen)
——移动字符
函数移动字符(事件)
如果event.phase==“开始”,则
如果event.x>character.x+8,则
转换到(背景,{time=800,x=background.x-16})
结束
如果事件x
您可以尝试以下方法:

function moveCharEF()
     if event.x > character.x+8 then
         background.x = background - 16
     end    
     if event.x < character.x-8 then
         background.x = background + 16
     end
end

function moveCharacter(event)
    if  event.phase == 'began' then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
        Runtime:addEventListener( "enterFrame", moveCharEF )
    elseif event.target.isFocus then
        if event.phase == "ended" then
            Runtime:removeEventListener( "enterFrame", moveCharEF )
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
        end
    end
end

function touchScreen(event)
    Runtime:removeEventListener('touch', moveCharacter)

    if  event.phase == 'began' then
        Runtime:addEventListener('touch', moveCharacter)
    end

end

Runtime:addEventListener('touch', touchScreen) 
函数moveCharEF()
如果event.x>character.x+8,则
background.x=background-16
结束
如果事件x

顺便说一下,我不知道你的申请。所以角色可能移动得太快或太慢。只需更改moveCharEF()函数的相关行即可

这就是您要查找的

local isTransitionInProgress = false

local function resetFlag()
   isTransitionInProgress = false
end

function moveCharacter(event)
   if(event.x > character.x+8 and isTransitionInProgress==false)  then
     isTransitionInProgress = true
     transition.to(background, {time=800, x=background.x-16,onComplete=resetFlag()})
   end

   if(event.x < character.x-8 and isTransitionInProgress==false) then
     isTransitionInProgress = true
     transition.to(background, {time=800, x=background.x+16,onComplete=resetFlag()})
   end
end

background:addEventListener("tap", moveCharacter)
local isTransitionInProgress=false
本地函数resetFlag()
isTransitionInProgress=错误
结束
函数移动字符(事件)
如果(event.x>character.x+8且isTransitionInProgress==false),则
isTransitionInProgress=真
转换到(后台,{time=800,x=background.x-16,onComplete=resetFlag()})
结束
如果(event.x
继续编码…:)

您可以尝试使用“点击”监听器而不是“触摸”。它一次只记录一次触摸

Runtime:addEventListener('tap', touchScreen)