Sprite animation question

Hello all,

I am working on an app where the user needs to move from one scene to the next by tapping on objects on the screen. To make it clear that the user needs to tap, I thought I could show a tapping finger animation. Since such animation would be used on a number of scenes, it makes sense to put it in a global module and call it from there.

I did write a function inside a module and I added a sprite animation to it and I added a timer to remove the sprite after 2 seconds. Then I call that function from the “show” event of a scene.

This works well, until the user clicks too quickly (while the animation is still playing) and moves to a new scene where the animation function is called again. I am getting the following error: 

attempt to call method 'removeSelf' (a nil value)

My code is as follows:

local M = {} local tap\_animation local function remove\_tap\_finger() tap\_animation:removeSelf() end -- Shows a finger, tapping on the screen for a short period. function M.show\_tap\_finger() local sheetData = { width=100, height=143, numFrames=2, sheetContentWidth=200, sheetContentHeight=143 } local tap\_sheet = graphics.newImageSheet( "graphics/tap\_sheet.png", sheetData ) local sequenceData = {{ name="seq1", sheet=sheet1, frames= { 1, 2, 1, 2, 1 }, time=1500, loopCount=1 },} tap\_animation = display.newSprite( tap\_sheet, sequenceData ) tap\_animation.x = display.contentCenterX ; tap\_animation.y = display.contentCenterY tap\_animation:play() timer.performWithDelay( 2000, remove\_tap\_finger ) end

Another problem with the function above is that when I move to the next scene before the animation is finished, the animation continues in the next scene. I want the animation to stop (and object removed) if the scene changes.

I know I can solve this by adding the animation function to each scene and adding the sprite to the sceneGroup of that scene, but is there a way to do it globally so that I don’t have to rewrite the function in every scene?

Thanks.

Hi Anmar71,

I’ve a similar situation going on in my game using overlays.

Are you able to restrict the ability to click on objects whilst the animation is doing it’s thing.
Not sure if this is the correct way to resolve it, but its the path I am taking in my game…

EG

In my overlay scene I have:

SCENE SHOW WILL:
 

    local parent = event.parent  – Reference to the parent scene object    

    if ( phase == “will” ) then

        – Code here runs when the scene is still off screen (but is about to come on screen)

        print(“OL5 SHOW EVENT WILL”) --testing

        – ================================================================================================

        – MANAGE PARENT SCENE

        – hide the parent scenes groupMain ( so all hit navigation objects and the btnBackPackClosed are disabled and invisable )

        parent:disableGroupMain()

        parent:disableGroupUserInterface()

        parent = nil

       

Then in the parent scene, I have…
 

– -----------------------------------------------------------------------------------

– GLOBAL FUNCTIONS SPECIFIC TO THIS PARENT SCENE

– -----------------------------------------------------------------------------------

– create global functions that live in this scene,

– so that the OL1-InventoryBag can call them to hide/show the groupMain

function scene:disableGroupMain() groupMain.isVisible = false end

function scene:enableGroupMain() groupMain.isVisible = true end

– so that the OL2-Notifications can call them to hide/show the groupUserinterface (also OL2-Notifications)

function scene:disableGroupUserInterface() 

    print(“disableGroupUserInterface”)

    

    groupUserInterface.isVisible = true

    groupUserInterface.isHitTestable = false 

    groupMain.isVisible = true

    groupMain.isHitTestable = false

end

function scene:enableGroupUserInterface() groupUserInterface.isHitTestable = true end

I am currently testing the ability to turn off the objects own ability to receive hits, whilst the overlay is doing it’s thing, this then prevents players from moving to another scene whilst my popup message is on screen.

I’ve not yet done anything with Sprites, so not sure if this approach, is even possible for you with the animations, but from what I gather, you need a way to control the underlaying objects, that is managed by the animation.

The other thing that comes to mind, is that an Overlay is destroyed when it is closed. Meaning the destroy and hide scene events are guaranteed to be run, so could you call to removeself() the objects then? If needs be put the function to call in the parent scene where the object lives and using the parent event to call it from the overlay??

I hope these ideas give you some food for thought.
Good luck :slight_smile:
Angela

 

@AngelaMcCall

I guess I could use a (global) flag that I set when the animation starts and clear when it finishes, then use that flag to disable/enable “tap” events in the scene. I will give it a try.

Thanks Angela.