1

当我单击它时,我正在尝试使用 spritesheet 来更改角色的图像,但是使用触摸会不止一次地运行该功能。这可能是一个愚蠢的问题,但我昨天开始了......这是代码,谢谢!:)

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

尝试使用tap, 而不是 touch:

 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

更多信息:触摸事件阶段检测对象和屏幕点击

继续编码... :)

于 2013-04-22T04:23:05.617 回答