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