当我点击它时,我正在尝试使用spritesheet来更改角色的图像,但是使用touch会不止一次地运行该函数。这可能是一个愚蠢的问题,但我昨天开始了......这是代码,谢谢! :)
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)'
答案 0 :(得分:0)
尝试使用tap
,而不是触摸:
firstbase:addEventListener("tap", changeFrame)
因为touch
有3个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
了解详情:Touch Event Phases和Detecting Object and Screen Taps
继续编码......:)