Best practice for stopping/starting sprite animations for game pause/un-pause?

Hi All,

Does anyone have any advice regarding how to handle pausing and un-pausing my sprite animations when I pause/un-pause my game?

I’m assuming that I’ll use the object:pause() and object:play() methods?

The trick is keeping track of all the objects on screen that currently are animating so that I can stop them when required.

Should I add every object to a master table when I create them and then cycle through that table pausing and un-pausing all members as appropriate?

When an object is removed I’ll remove it from that table as well.

Is there a better approach? I wanted to bounce this off some people to see if I was on the right track.

Thanks :slight_smile:

Hi @EHO,

Your approach is essentially correct. I usually add my animated sprites to a table like “animObjects” and then, when I remove one particular animated object from the scene/stage (or if I pause it), I delete it from the table as follows (this is just one method; you can write your own):

[lua]

local function removeID( tableID, objID )

    for i=1,#tableID do

        local v = tableID[i] ; if ( v and v == objID ) then table.remove( tableID,i ) ; break end ; v = nil

    end

end

removeID( animObjects, myObject )

[/lua]

Or, loop through the entire table to pause/play everything:

[lua]

if ( #animObjects > 0 ) then for j=1,#animObjects do animObjects[j]:play() end end

[/lua]

I suppose the “if” check isn’t really necessary, as Lua will just continue on its way in the case of a loop of 0 items.

Hi @EHO,

Your approach is essentially correct. I usually add my animated sprites to a table like “animObjects” and then, when I remove one particular animated object from the scene/stage (or if I pause it), I delete it from the table as follows (this is just one method; you can write your own):

[lua]

local function removeID( tableID, objID )

    for i=1,#tableID do

        local v = tableID[i] ; if ( v and v == objID ) then table.remove( tableID,i ) ; break end ; v = nil

    end

end

removeID( animObjects, myObject )

[/lua]

Or, loop through the entire table to pause/play everything:

[lua]

if ( #animObjects > 0 ) then for j=1,#animObjects do animObjects[j]:play() end end

[/lua]

I suppose the “if” check isn’t really necessary, as Lua will just continue on its way in the case of a loop of 0 items.