【问题标题】:Using Corona, it runs through the function more than once使用 Corona,它不止一次地运行函数
【发布时间】:2013-04-19 16:10:17
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios function touch coronasdk


    【解决方案1】:

    尝试使用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 PhasesDetecting Object and Screen Taps

    继续编码... :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多