1

スプライトシートを使用して、クリックしたときにキャラクターの画像を変更しようとしていますが、タッチを使用すると機能が複数回実行されます。これはばかげた質問かもしれませんが、私は昨日始めたので...これがコードです、ありがとう! :)

local sprite = require "sprite"
local changed = 0

local baseballfield = display.newImageRect("baseballfield.png",display.contentWidth,display.contentHeight)
baseballfield.x = display.contentWidth/2
baseballfield.y = display.contentHeight/2

local spritesheet = sprite.newSpriteSheet("spritesheet.png",119,152)
local players = sprite.newSpriteSet(spritesheet, 1, 2)
local firstbase = sprite.newSprite(players)

function changeFrame(event)
    if firstbase.currentFrame == 1 then
firstbase.currentFrame = 2
changed = 1
end
if firstbase.currentFrame == 2 and changed == 0 then
firstbase.currentFrame = 1
changed = 1
end
changed = 0
end

firstbase:addEventListener("touch", changeFrame)'
4

1 に答える 1

0

taptouch の代わりに ,を使用してみてください。

 firstbase:addEventListener("tap", changeFrame)

touch3 つの events.phases があるため、関数はこれら 3 つのイベントで呼び出されます。彼らです:

 1) began  -- your function will get called when the touch begins
 2) moved  -- your function will get called when you touch and move
 3) ended  -- your function will get called when the touch ends

詳細については、「タッチ イベント フェーズ」 および「オブジェクトと画面のタップの検出」を参照してください。

コーディングを続けてください... :)

于 2013-04-22T04:23:05.617 に答える