how to cancel a function?

I have a function. – it does some transitions to several objects, with alpha = 1 to alpha = 0

I tap a button – call the function – and the function makes the transitions… Perfect!

after it’s done, I tap the button again, and everything works fine…!

------The problem-------

if kids push another button in the middle of the way,

before the function has finish doing all the transitions…

and the go to the main Menu…

when they come back to the previews scene where I have the function…

I don’t see the transitions alpha, and the function it’s not working…

but if I go to another page, and comeback the second time

then the functions works prefect!


how can I fix this?

I think it’s has to do with the scens enterScene or something

I’m not sure.


this is the code

-- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- -- Piano For Kids - Level 1 -- By Victor M. Barba -- Copyright 2013 -- All Rights Reserved -- -- Version 2.0 -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- local storyboard = require( "storyboard" )-- --==\*\*\*\*\*\*\*\*\*\*[STORYBOARD REQUIRE]\*\*++-- -- local widget = require "widget" local scene = storyboard.newScene() local chord = audio.loadSound( "acordeDo.mp3")-- --==\*\*\*\*\*\*\*\*\*\*\*[LOAD SOUNDS]\*\*++-- -- audio.setVolume( 5 ) audio.setVolume( 0.5, { channel=1 } ) -- for BackgroundMusic audio.setVolume( 0.9, { channel=2 } ) -- for Music local buttonBack -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[VARIABLES]\*\*++-- -- local buttonHome local buttonPlay local dot1 local dot2 local dot3 local moveDots local songIsPlaying = false local dotIsVisible = false local function buttonBackHandler() -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[FUNCTIONS]\*\*++-- --         storyboard.gotoScene( "mainChords", "fromLeft", 200 )     return true end local function buttonHomeHandler()         storyboard.gotoScene( "home", "fromLeft", 200 )         return true end function moveDots ()     if dotIsVisible == false then         transition.to(dot1, {time=10, delay=10, alpha=1})         transition.to(dot1, {time=10, delay=500, alpha=0})         transition.to(dot2, {time=10, delay=500, alpha=1})         transition.to(dot2, {time=10, delay=1100, alpha=0})         transition.to(dot3, {time=10, delay=1300, alpha=1})         transition.to(dot3, {time=10, delay=1800, alpha=0})         transition.to(dot2, {time=10, delay=2000, alpha=1})         transition.to(dot2, {time=10, delay=2600, alpha=0})         transition.to(dot1, {time=10, delay=2700, alpha=1})         transition.to(dot1, {time=10, delay=5000, alpha=0})         transition.to(dot1, {time=20, delay=5300, alpha=1})         transition.to(dot2, {time=20, delay=5300, alpha=1})         transition.to(dot3, {time=20, delay=5300, alpha=1})              transition.to(dot1, {time=10, delay=5500, alpha=0})         transition.to(dot2, {time=10, delay=5500, alpha=0})         transition.to(dot3, {time=10, delay=5500, alpha=0})         transition.to(dot1, {time=30, delay=5900, alpha=1})         transition.to(dot2, {time=30, delay=5900, alpha=1})         transition.to(dot3, {time=30, delay=5900, alpha=1})              transition.to(dot1, {time=10, delay=6300, alpha=0})         transition.to(dot2, {time=10, delay=6300, alpha=0})         transition.to(dot3, {time=10, delay=6300, alpha=0})         transition.to(dot1, {time=30, delay=6600, alpha=1})         transition.to(dot2, {time=30, delay=6600, alpha=1})         transition.to(dot3, {time=30, delay=6600, alpha=1})              transition.to(dot1, {time=10, delay=7000, alpha=0})         transition.to(dot2, {time=10, delay=7000, alpha=0})         transition.to(dot3, {time=10, delay=7000, alpha=0})         transition.to(dot1, {time=30, delay=7350, alpha=1})         transition.to(dot2, {time=30, delay=7350, alpha=1})         transition.to(dot3, {time=30, delay=7350, alpha=1})              transition.to(dot1, {time=10, delay=7700, alpha=0})         transition.to(dot2, {time=10, delay=7700, alpha=0})         transition.to(dot3, {time=10, delay=7700, alpha=0})         transition.to(dot1, {time=30, delay=8100, alpha=1})         transition.to(dot2, {time=30, delay=8100, alpha=1})         transition.to(dot3, {time=30, delay=8100, alpha=1})              transition.to(dot1, {time=10, delay=11000, alpha=0})         transition.to(dot2, {time=10, delay=11000, alpha=0})             local function resetDot ()                 dotIsVisible = false             end         transition.to(dot3, {time=10, delay=11000, alpha=0, onComplete=resetDot})         dotIsVisible = true     end     return true end local function buttonPlayHandler()     moveDots ()     if songIsPlaying == false then             local function reset ()                 songIsPlaying = false             end         audio.play(chord, {onComplete=reset})         songIsPlaying = true     end     return true end function scene:createScene(event) -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*++-- --     local group = self.view          local background = display.newImage( "backgroundChordC.png" )     group:insert (background)          dot1 = display.newCircle( 680, 430, 12)     group:insert (dot1)     dot1:setFillColor(255,255,0)     dot1.strokeWidth = 2     dot1:setStrokeColor(128,0,0)     dot1.alpha = 0          dot2 = display.newCircle( 735, 430, 12)     group:insert (dot2)     dot2:setFillColor(255,255,0)     dot2.strokeWidth = 2     dot2:setStrokeColor(128,0,0)     dot2.alpha = 0          dot3 = display.newCircle( 790, 430, 12)     group:insert (dot3)     dot3:setFillColor(255,255,0)     dot3.strokeWidth = 2     dot3:setStrokeColor(128,0,0)     dot3.alpha = 0     buttonBack = widget.newButton{         defaultFile="buttonBack.png",         onRelease = buttonBackHandler     }     group:insert ( buttonBack )     buttonBack.x = 400     buttonBack.y = 680          buttonHome = widget.newButton{         defaultFile="buttonHome.png",         onRelease = buttonHomeHandler     }     group:insert ( buttonHome )     buttonHome.x = 550     buttonHome.y = 680          buttonPlay = widget.newButton{         defaultFile="buttonPlay.png",         onRelease = buttonPlayHandler     }     group:insert ( buttonPlay )     buttonPlay.x = 700     buttonPlay.y = 680 end function scene:enterScene(event) -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*++-- --     local group = self.view      end function scene:exitScene(event) -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*++-- --     local group = self.view          audio.stop ()      end function scene:destroyScene(event) -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*++-- --     local group = self.view         if buttonBack then         buttonBack:removeSelf()         buttonBack = nil     end          if buttonHome then         buttonHome:removeSelf()         buttonHome = nil     end          if buttonPlay then         buttonPlay:removeSelf()         buttonPlay = nil     end end ------------------------------------------------------------------------------------------ scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thank you very much for your help