setSequence problem

Hi. Could somebody save my sanity please?

I am attempting a very simple sprite animation and would like the sprite to change from one sequence to another.

I am using setSequence to no effect. As below . . . 

First sequence runs fine. Print statements confirm the sequence has not changed

[lua]

print “*************** HERE1 **************************”
local tmp
local  boatInfo = require “ch2CH”
local boatSheet = graphics.newImageSheet(“ch2CH.png”, boatInfo:getSheet() )
local boatSeqData

boatSeqData =
 {
  {name = “setsail”, frames  = {3,8}, time = 200, loopCount = 2, loopDirection = “forward”},
 },
 {
   {name = “setsail2”, frames = {1 ,2, 3 ,4,5,6,7,8 }, time = 600}
 }
 
 
– Create sprite
tmp = display.newSprite( boatSheet, boatSeqData )
tmp.x = display.contentCenterX
tmp.y =  display.contentCenterY

tmp:setSequence( “setsail” )
tmp:play()

print(“XXX1” … tostring(tmp.sequence))  
tmp:setSequence( “setsail2” )
print(“XXX2” … tostring(tmp.sequence))
tmp:play()
print “*************** HERE2 **************************”

[/lua]

You may be running into a timing issue. Most Corona API’s do work then return when done. But some API calls take time to complete like making a network request that could take seconds to complete. Those API calls return immediately and finish their work in the background and notify you when they are complete. These are called asynchronous APIs.

When you play a sprite, it takes time to complete the play sequence. Your first sequence is 2 frames, 200ms per frame looping twice, so that’s 800ms to complete. We don’t want to freeze your program while that sprite plays so we return control to the next line of code immediately and run the animation in the background.

The documentation for the sprite object’s .sequence attribute says it has the value of the currently playing sequence. By the time you get to line 26, the first sequence should still be playing. The sprite code supports an event listener that triggers when things like sequences end. You can use that information to play your next sequence when the first is complete.  See:

https://docs.coronalabs.com/api/type/SpriteObject/play.html

Rob

Ah yes. Good point ​ will try that and let you know

This is wrong:

boatSeqData = { {name = "setsail", frames = {3,8}, time = 200, loopCount = 2, loopDirection = "forward"}, }, { {name = "setsail2", frames = {1 ,2, 3 ,4,5,6,7,8 }, time = 600} }

Corrected:

boatSeqData = { {name = "setsail", frames = {3,8}, time = 200, loopCount = 2, loopDirection = "forward"}, {name = "setsail2", frames = {1 ,2, 3 ,4,5,6,7,8 }, time = 600}, }

Thanks roaminggamer

You may be running into a timing issue. Most Corona API’s do work then return when done. But some API calls take time to complete like making a network request that could take seconds to complete. Those API calls return immediately and finish their work in the background and notify you when they are complete. These are called asynchronous APIs.

When you play a sprite, it takes time to complete the play sequence. Your first sequence is 2 frames, 200ms per frame looping twice, so that’s 800ms to complete. We don’t want to freeze your program while that sprite plays so we return control to the next line of code immediately and run the animation in the background.

The documentation for the sprite object’s .sequence attribute says it has the value of the currently playing sequence. By the time you get to line 26, the first sequence should still be playing. The sprite code supports an event listener that triggers when things like sequences end. You can use that information to play your next sequence when the first is complete.  See:

https://docs.coronalabs.com/api/type/SpriteObject/play.html

Rob

Ah yes. Good point ​ will try that and let you know

This is wrong:

boatSeqData = { {name = "setsail", frames = {3,8}, time = 200, loopCount = 2, loopDirection = "forward"}, }, { {name = "setsail2", frames = {1 ,2, 3 ,4,5,6,7,8 }, time = 600} }

Corrected:

boatSeqData = { {name = "setsail", frames = {3,8}, time = 200, loopCount = 2, loopDirection = "forward"}, {name = "setsail2", frames = {1 ,2, 3 ,4,5,6,7,8 }, time = 600}, }

Thanks roaminggamer