Stopping animation sequence from changing in the middle of animation

I have an animation sequence that I am changing in a function which is called from onComplete from transition.to.  The problem is that if this happens to get called when my animation sequence is in the middle, it seems like it is not finishing out the sequence, but immediately changing to the next sequence and it does not look right. 

I would like to finish out the current animation frames (just one iteration) so that it ends at the end of the sequence before starting the next one. 

Is there a way to do this?

Currently in the transition.to onComplete function I have this code:

local function stopAnim()

bb_anim:setSequence(“stand”)

bb_anim:play()

end

When it switches to the new “stand” sequence it sometimes looks weird because the prior sequence did not finish.  I’m sure there is a very easy way to do this but I wasn’t having any luck. 

Thanks!

I would probably approach this via eventListeners, see
https://docs.coronalabs.com/guide/media/spriteAnimation/index.html#sprite-events.

First, in your “stopAnimation” function, all you need to do is to add a boolean value to your object, e.g. “object.stopAnimation = true”.

Then, create a simple eventListener for your sprite(s) that checks if the event phase is ended and stopAnimation is true for the object, then change the sequence and play.

local function stopAnim(x, y) bb\_anim.stopAnimation=true end 

I gave that a shot but it seems I have a problem with the event listener part of what you said.  It is now not stopping the animation at all.  I put some print statements and it looks like event.phase is never “ended”. 

06:49:23.304  bbanim.stopAnimimation true

06:49:23.408  event. phase next

06:49:23.408  bbanim.stopAnimimation true

06:49:23.509  event. phase loop

06:49:23.509  bbanim.stopAnimimation true

06:49:23.611  event. phase next

06:49:23.611  bbanim.stopAnimimation true

06:49:23.705  event. phase next

06:49:23.705  bbanim.stopAnimimation true

06:49:23.808  event. phase loop

Attached some of my code below if it helps.

local function spriteListener(event) --print("event. phase " .. event.phase) --print("bbanim.stopAnimimation " .. tostring(bb\_anim.stopAnimation)) if event.phase == ended and bb\_anim.stopAnimation == true then bb\_anim:setSequence("stand") bb\_anim:play() end 

local sheetData = { width = 54, height=62, numFrames=2, sheetContentWidth=109, sheetContentHeight=63} local imageSheet = graphics.newImageSheet("Levels/Sprites/bb\_ss.png", sheetData) local sequenceData = { { name="stand", start=1, count=1}, { name = "bounce", frames = {1,2,1}, time=300} } local bb\_anim = display.newSprite(imageSheet, sequenceData) bb\_anim.x = bb\_position.x bb\_anim.y = bb\_position.y bb\_anim.stopAnimation=false

First of all, the event.phase value is a string, so you’d need to check for “ended” and not ended.

If you are running an animation on a constant loop and it never reaches “ended” phase, then use one of the other phases, like “loop”.

Edit:

If you read to the end of the documentation section that I linked, it reads:

"Notice that the sprite listener transmits all phases to the listener function, so it’s your responsibility to use conditional clauses and perform an appropriate action when a specific phase occurs in the animation sequence. In the above example, only the ended phase is detected, which occurs after 4 complete loops of the normalRun sequence. When that sequence ends, the fastRun sequence is set and played, and because it loops indefinitely, no additional ended phase will ever occur.

So, if you use an endlessly looping sequence, you’ll need to look for “loop” rather than “ended”.

Thank you for pointing that out.  I will try it with “loop” when I get home from work today.