Sprite animation stuck after setSequence

Hi,

My problem is when I change the sequence in my sprite animation with

obj:setSequence("fastRun"); obj:play()

the animation change is not smooth, it stucks for 1 second at the first frame then plays as expected.

How can I do a smooth sequence change?

Can you post your sprite sequence code as well? That will help to evaluate the issue.

It’s in the createScene function.

local sheetData = {width = 225, height = 350, numFrames = 3, sheetContentWidth = 675, sheetContentHeight = 350} local mySheet = graphics.newImageSheet(\_G.images .. "\_sprite\_run.png", sheetData) local sequenceData = {         {name = "walk", start = 1, count = 3, time = 800, loopCount = 0, loopDirection = "forward"}, {name = "run", start = 1, count = 3, time = 500, loopCount = 0, loopDirection = "forward"}, {name = "runFast", start = 1, count = 3, time = 300, loopCount = 0, loopDirection = "forward} } obj = display.newSprite(mySheet, sequenceData) obj:setSequence("walk") obj.x = 450 obj.y = 200 obj.name = "player" obj:play()

The sequence change is in the enterScene function.

Here I got a runtime listener with the animate function.

Runtime:addEventListener("enterFrame", animate);

In the animate function the sequence change like this

if dist % 5 == 0 then --just for testing it multiple times runSpeed = runSpeed + 1 obj:setSequence("run") obj:play() end

The enterScene call is made a few seconds after the scene is loaded, so that is why you’re seeing a delay. FYI, having the sprite code in the createScene call isn’t the best practice. You would be better off moving it outside of any scene call, wrapping it in a function, and calling it from within a scene that way. Try something like this:

local function callPlayerTest() local sheetData = {width = 225, height = 350, numFrames = 3, sheetContentWidth = 675, sheetContentHeight = 350} local mySheet = graphics.newImageSheet(\_G.images .. "\_sprite\_run.png", sheetData) local sequenceData = { {name = "walk", start = 1, count = 3, time = 800, loopCount = 0, loopDirection = "forward"}, {name = "run", start = 1, count = 3, time = 500, loopCount = 0, loopDirection = "forward"}, {name = "runFast", start = 1, count = 3, time = 300, loopCount = 0, loopDirection = "forward} } obj = display.newSprite(mySheet, sequenceData) obj:setSequence("walk") obj.x = 450 obj.y = 200 obj.name = "player" obj:play() return obj end -- in your enterScene or createScene code do the following: local player = callPlayerTest()

I tried your suggestion, the results are the same. but I found something interesting.

If I put the setSequence outside from the animate function (for example at button press) it works fine,

so the problem must be with the animate function.

ah, I didn’t even look at your enterFrame function, I assumed that wasn’t being used as it didn’t have the “runFast” sequence set inside it, which I believe was the called sequence in your original issue. 

The animate function is my game loop, and when the player reaches a given distance the sprite sequence should change to “run” seq smoothly.

It was my mistake, in the animate function I loaded the setSequence multiple times consecutively, that causes the stuck.

Thank you for the help, this pointed me in the right direction.

Can you post your sprite sequence code as well? That will help to evaluate the issue.

It’s in the createScene function.

local sheetData = {width = 225, height = 350, numFrames = 3, sheetContentWidth = 675, sheetContentHeight = 350} local mySheet = graphics.newImageSheet(\_G.images .. "\_sprite\_run.png", sheetData) local sequenceData = {         {name = "walk", start = 1, count = 3, time = 800, loopCount = 0, loopDirection = "forward"}, {name = "run", start = 1, count = 3, time = 500, loopCount = 0, loopDirection = "forward"}, {name = "runFast", start = 1, count = 3, time = 300, loopCount = 0, loopDirection = "forward} } obj = display.newSprite(mySheet, sequenceData) obj:setSequence("walk") obj.x = 450 obj.y = 200 obj.name = "player" obj:play()

The sequence change is in the enterScene function.

Here I got a runtime listener with the animate function.

Runtime:addEventListener("enterFrame", animate);

In the animate function the sequence change like this

if dist % 5 == 0 then --just for testing it multiple times runSpeed = runSpeed + 1 obj:setSequence("run") obj:play() end

The enterScene call is made a few seconds after the scene is loaded, so that is why you’re seeing a delay. FYI, having the sprite code in the createScene call isn’t the best practice. You would be better off moving it outside of any scene call, wrapping it in a function, and calling it from within a scene that way. Try something like this:

local function callPlayerTest() local sheetData = {width = 225, height = 350, numFrames = 3, sheetContentWidth = 675, sheetContentHeight = 350} local mySheet = graphics.newImageSheet(\_G.images .. "\_sprite\_run.png", sheetData) local sequenceData = { {name = "walk", start = 1, count = 3, time = 800, loopCount = 0, loopDirection = "forward"}, {name = "run", start = 1, count = 3, time = 500, loopCount = 0, loopDirection = "forward"}, {name = "runFast", start = 1, count = 3, time = 300, loopCount = 0, loopDirection = "forward} } obj = display.newSprite(mySheet, sequenceData) obj:setSequence("walk") obj.x = 450 obj.y = 200 obj.name = "player" obj:play() return obj end -- in your enterScene or createScene code do the following: local player = callPlayerTest()

I tried your suggestion, the results are the same. but I found something interesting.

If I put the setSequence outside from the animate function (for example at button press) it works fine,

so the problem must be with the animate function.

ah, I didn’t even look at your enterFrame function, I assumed that wasn’t being used as it didn’t have the “runFast” sequence set inside it, which I believe was the called sequence in your original issue. 

The animate function is my game loop, and when the player reaches a given distance the sprite sequence should change to “run” seq smoothly.

It was my mistake, in the animate function I loaded the setSequence multiple times consecutively, that causes the stuck.

Thank you for the help, this pointed me in the right direction.