onComplete in sprite animation

So I have a sprite which I want to make it having an attack pattern each time I pressed on attack button. I want to use onComplete function but it doesn’t call anything. I get this idea from chatGPT. I don’t get it why onComplete doesn’t get called. I already put my sequence loopCount = 1.

function Hero:playAttSeq()
        print("wooooo")
        print(#attackpattern)
        print(json.prettify(attackpattern))
        Hero:setSequence("attack-"..attackpattern[1])
        Hero:play({ onComplete = function() print("done") end})
    end

Hi,
It’s a bit more complex than that because you have to use a listener:

https://docs.coronalabs.com/api/event/sprite/phase.html

Here’s something you can do once for all your sprites:

local function spriteListener( event )
    if event.phase == "ended" and event.target.onComplete then
         event.target.onComplete()
    end
end

local function myNewSprite(onComplete, params for sprite)
    local obj = real new sprite...

    obj.onComplete=onComplete
    
    obj:addEventListener( "sprite", spriteListener )

   return obj
end

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.