0

我是 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)
4

3 回答 3

0

您可以尝试使用“tap”侦听器而不是“touch”。它一次只记录一次触摸。

Runtime:addEventListener('tap', touchScreen)
于 2013-04-29T02:52:03.750 回答
0

是你要找的吗..?

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)

继续编码... :)

于 2013-04-23T09:05:44.870 回答
0

你可以试试这个:

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() 函数的相关行

于 2013-04-23T00:46:22.910 回答