在corona中,如何让精灵表动画播放到结束,然后才能再次播放?

我有一个点击按钮可以让我的角色出拳,但如果快速连续点击,我的角色会在完成拳击动画之前不断出拳,以至于他会奇怪地抽搐...我已经调试了几个小时,但似乎无法解决。

我尝试在点击后移除按钮事件监听器,但速度不够快(在它启动之前我仍然能够点击两三次)。

我试着在顶部设一个”ispunching”变量,在动画播放时将其切换为真,但在这之前我仍然可以发出几次点击。

我知道可能有一种容易的方法,但我太蠢了!感谢任何帮助!

精灵数据和顺序:

local sheetData1 = { width=175, height=294, numFrames=11, sheetContentWidth=1925, sheetContentHeight=294}
local sheet1 = graphics.newImageSheet( "guy.png", sheetData1 )

local sheetData2 = { width=220, height=294, numFrames=4, sheetContentWidth=880, sheetContentHeight=294 }
local sheet2 = graphics.newImageSheet( "guy2.png", sheetData2 )

local sheetData3 = { width=261, height=300, numFrames=8, sheetContentWidth=2088, sheetContentHeight=300 }
local sheet3 = graphics.newImageSheet( "guy3.png", sheetData3 )

local sequenceData = {
{ name="walk", sheet=sheet1, start=5, count=4, time=800, loopCount=0 },
{ name="idle", sheet=sheet1, frames={ 1,2,3,4 }, time=2000, loopCount=0 },
{ name="punch", sheet=sheet2, start=1, count=4, time=400, loopCount=1 },
{ name="kick", sheet=sheet3, start=1, count=4, time=400, loopCount=1 },
{ name="kick2", sheet=sheet3, start=5, count=4, time=400, loopCount=1 },
{ name="jump", sheet=sheet1, start=9, count=3, time=400, loopCount=1 }
}

角色:

guy = display.newSprite( group, sheet1, sequenceData )
physics.addBody( guy, "static", { friction=0.5, bounce=0 } )
guy.x = 600
guy.y = 600

空闲姿势:

local function idlePose()
guy:setSequence( "idle" )
guy:play()

end

显示按钮:

local btn1 = display.newImage ("button1.png")
btn1.x = 1100
btn1.y = 510
btn1:scale (1.5,1.5)
btn1.alpha=0.5
group:insert( btn1 )

按钮代码:

local function onTap( event )

if guy.sequence == "punch" and guy.isPlaying == true then
print("isplaying")
return
else
print("notplaying")
guy:setSequence( "punch" )
guy:play()
timer.performWithDelay(400, idlePose)
end
end
btn1:addEventListener("tap", onTap)
点赞
用户1091208
用户1091208

Sprite 对象有一个 isPlaying 属性,在你轻拍时可以检查:

local function onTap(event)
    if guy.isPlaying then
        return
    end

    guy:setSequence("punch")
    guy:play()

    timer.performWithDelay(400, idlePose)
end

btn1:addEventListener("tap", onTap)

如果你只想阻止他反复挥拳(也就是他还可以做其他事情),那么你也可以检查 sequence 属性:

if 条件从 guy.isPlaying 更改为 guy.isPlaying and guy.sequence == "punch" 将只在他挥拳时停止事件再次触发,但是将允许其他事件被覆盖。 比如,如果你的角色正在奔跑,而你希望他在按下挥拳按钮后立刻挥拳,而不是等待奔跑动画的结束。

2014-01-11 12:00:36