我是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)
答案 0 :(得分: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()函数的相关行
即可答案 1 :(得分: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)
继续编码......:)
答案 2 :(得分:0)
您可以尝试使用“点击”侦听器而不是“触摸”。它一次只能记录一次触摸。
Runtime:addEventListener('tap', touchScreen)