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.