0

Lua は初めてで、キャラクターの動きをシミュレートしようとしています。現在、キャラクターを左右に動かしています。キャラクターを一度に 16 ピクセルずつ動かしたいと考えています。ユーザーが電話にすばやく触れないことを考えると、これはうまく機能します。その場合、キャラクターはランダムな数のピクセルを移動します。

私の質問は、タッチ イベントを一度に 1 回だけ登録するにはどうすればよいかということです。

私のコード:

-- 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

それはあなたが探しているものですか..?

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 に答える