我是Lua的新手,我试图模拟角色移动。 我现在有角色左右移动。我希望角色一次移动16个像素。如果用户没有迅速触摸手机,这可以正常工作。在这种情况下,角色会移动一个随机数的像素。用Lua移动一个角色
我的问题是,我如何获得触摸事件,一次只注册一次。
我的代码:
-- 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)