How to restart sprite animation from the begining everytime i go to scene?

every time i go to another scene the animation gone but when i comeback to the scene, the animation not start from the begining. how to stop the sprite animation everytime i goto another scene,then when i comeback the animation restart again from the 1st frame. It is only two frames in my imagesheets.

Thanks in advance.

here is my code

function scene:create( event )
    local sceneGroup = self.view
    
    image = display.newImage( “betul.png” )
    image.x = display.contentCenterX
    image.y = display.contentCenterY
    
    sceneGroup:insert( image )
    
    image.touch = onSceneTouch
    ------------------------
    -----------------------
    image2 = display.newImage( “fikir.gif” )
    image2.x = display.contentCenterX
    image2.y = display.contentCenterY
    image2.isVisible=true
    sceneGroup:insert( image2 )
    

– an image sheet with a character
local sheet1 = graphics.newImageSheet( “betul10.gif”, { width=400, height=470, numFrames=2 } )

– play 8 frames every 1000 ms
local instance1 = display.newSprite( sheet1, { name=“character”, start=1, count=2, time=5999 } )
instance1.x = display.contentWidth / 4 + 40
instance1.y =  75
instance1.xScale = .5
instance1.yScale = .5
instance1:play()
sceneGroup:insert( instance1 )

scene:create() may not be called every time you enter the scene. In fact it’s designed not to. We cache the scene if we have enough memory to save the toil of re-creating the scene all the time.

While it’s okay to create the animation in scene:create(), it really should be started and stopped/paused in scene:show() and scene:hide(). These two scenes are guaranteed to trigger each time  you enter and leave a scene. In fact, they are triggered twice, once when the before the scene comes on the screen (scene:show() event.phase == “will”) and once after the scene is on screen (scene:show() event.phase == “did”). scene:hide() is the same except that the phases are backwards, “will” will happen before the scene leaves the screen, “did” after it’s off screen. You can call instance1:play() etc in these functions in the right phase. I would wait until the scene is on screen to start it, and pause/stop it before the scene leaves the screen.

You of course won’t be able to declare instance1 local inside of scene:create() because of scoping rules. Just forward declare it at the top:

local instance1

and when you create the instance leave off “local”

Rob

scene:create() may not be called every time you enter the scene. In fact it’s designed not to. We cache the scene if we have enough memory to save the toil of re-creating the scene all the time.

While it’s okay to create the animation in scene:create(), it really should be started and stopped/paused in scene:show() and scene:hide(). These two scenes are guaranteed to trigger each time  you enter and leave a scene. In fact, they are triggered twice, once when the before the scene comes on the screen (scene:show() event.phase == “will”) and once after the scene is on screen (scene:show() event.phase == “did”). scene:hide() is the same except that the phases are backwards, “will” will happen before the scene leaves the screen, “did” after it’s off screen. You can call instance1:play() etc in these functions in the right phase. I would wait until the scene is on screen to start it, and pause/stop it before the scene leaves the screen.

You of course won’t be able to declare instance1 local inside of scene:create() because of scoping rules. Just forward declare it at the top:

local instance1

and when you create the instance leave off “local”

Rob