So I have a scene where I have 3 images at the top - some arrows the user can use to change between them manually and a scroll view with text below.
Below that is a button which will play audio and automatically changes the images above at specific times in the audio.
However if I play the audio then leave the scene it seems the timers are started, when I leave the scene they dont seem to stop and the function they call still happens in which case the object doesnt exist anymore giving an error.
The bold line below is the one that comes up as an error - a nil object
function changeImage()
myCurrentImageC2:removeSelf()
myCurrentImageC2 = nil
myCurrentImageC2 = display.newImageRect( myImagesC2[2],240,181)
myCurrentImageC2.x = display.contentCenterX
myCurrentImageC2.y = display.contentCenterY-70
myCurrentImageC2.currentImageIndex = 2
sceneGroup:insert( myCurrentImageC2 )
end
print("location1") local leftSide = display.screenOriginX; local rightSide = display.contentWidth-display.screenOriginX; local topSide = display.screenOriginY; local bottomSide = display.contentHeight-display.screenOriginY; --------------------------------------------------------------------------------------- local composer = require "composer" local scene = composer.newScene() local widget = require "widget" math.randomseed(os.time()) local audioSound = audio.loadSound("audio/dublin/2\_northWilliamSteet.mp3") local isChannel1Playing = audio.isChannelPlaying( 1 ) local function btnTouch( event ) if event.phase == "ended" then print ("button pressed") composer.gotoScene(event.target.btnDestination, {time=250, effect="crossFade"}) return true end end local function myAudio( event ) if event.phase == "ended" then if isChannel1Playing == false then event.target.audioChannel = audio.play(audioSound,{ channel=1}) changeTimer = timer.performWithDelay( 40000, changeImage ) changeTimer2 = timer.performWithDelay( 60000, changeImage2 ) end return true end end -- Called when the scene's view does not exist: function scene:create( event ) local sceneGroup = self.view local bg = display.newImageRect("images/bg\_legacy.jpg", 360, 568) bg.x = display.contentCenterX bg.y = display.contentCenterY sceneGroup:insert( bg ) -- code to slide through locations -- myImagesC2 = {"images/Dublin/2a.png", "images/Dublin/2b.png", "images/Dublin/2c.png"} -- name of images currentImageIndex = 1 -- this is index to track your current image -- display the first image myCurrentImageC2 = display.newImageRect( myImagesC2[1],240,181) myCurrentImageC2.x = display.contentCenterX myCurrentImageC2.y = display.contentCenterY-70 myCurrentImageC2.currentImageIndex = 1 sceneGroup:insert( myCurrentImageC2 ) -- Function to handle back and forward buttons local function handleButtonEvent( event ) print( currentImageIndex ) if ( "ended" == event.phase ) then if event.target.id == "back" and currentImageIndex \> 1 then print( "Back" ) myCurrentImageC2:removeSelf() myCurrentImageC2 = nil myCurrentImageC2 = display.newImage( myImagesC2[currentImageIndex - 1]) myCurrentImageC2.x = display.contentCenterX myCurrentImageC2.y = display.contentCenterY-70 sceneGroup:insert( myCurrentImageC2 ) arrowLeft:toFront() arrowRight:toFront() currentImageIndex = currentImageIndex - 1 myCurrentImageC2.currentImageIndex = currentImageIndex elseif event.target.id == "fwd" and currentImageIndex \< 3 then print( "fwd" ) myCurrentImageC2:removeSelf() myCurrentImageC2 = nil myCurrentImageC2 = display.newImage( myImagesC2[currentImageIndex + 1]) myCurrentImageC2.x = display.contentCenterX myCurrentImageC2.y = display.contentCenterY-70 sceneGroup:insert( myCurrentImageC2 ) arrowLeft:toFront() arrowRight:toFront() currentImageIndex = currentImageIndex + 1 myCurrentImageC2.currentImageIndex = currentImageIndex end return true end end function changeImage() myCurrentImageC2:removeSelf() myCurrentImageC2 = nil myCurrentImageC2 = display.newImageRect( myImagesC2[2],240,181) myCurrentImageC2.x = display.contentCenterX myCurrentImageC2.y = display.contentCenterY-70 myCurrentImageC2.currentImageIndex = 2 sceneGroup:insert( myCurrentImageC2 ) end function changeImage2() myCurrentImageC2:removeSelf() myCurrentImageC2 = nil myCurrentImageC2 = display.newImageRect( myImagesC2[3],240,181) myCurrentImageC2.x = display.contentCenterX myCurrentImageC2.y = display.contentCenterY-70 myCurrentImageC2.currentImageIndex = 3 sceneGroup:insert( myCurrentImageC2 ) end -- Create a ScrollView local scrollView = widget.newScrollView { left = 0, top = display.contentCenterY+40, width = display.contentWidth, height = 150, topPadding = 10, bottomPadding = 20, id = "onBottom", horizontalScrollDisabled = true, verticalScrollDisabled = false, listener = scrollListener, hideBackground = true, } --Create a large text string local myText =[[Mary Aikenhead and her first companion Alicia Walsh, having completed their novitiate, arrived in Dublin from York on 22nd August 1815. They went to live in a house in North William Street, where they were to take over the care of a number of orphans. It was here that the two sisters made their first Profession of Vows on 1st September 1815. On that day Mary Aikenhead was appointed Superior General of the new congregation and Sr. (Mary) Catherine, novice mistress. Two days later they received the first postulant – Catherine Lynch from Drogheda. Two months after their arrival the sisters opened a day school, to which the poor children of the neighbourhood flocked in great numbers. Gradually the number of sisters increased and they were able to engage in the work originally envisaged by Mary Aikenhead – visitation of the sick poor in their homes. The words of Dr. Murray to Mary Aikenhead while she was still a novice in York had now become a reality: ‘Your family in future are to be the poor of Jesus Christ.’]] --Create a text object containing the large text string and insert it into the scrollView local myText = display.newText( myText, display.contentCenterX, 0, 300, 0, "Gotham-Book", 14) myText:setFillColor( 0 ) myText:setTextColor( 0, 0, 0 ) myText.anchorY = 0.0 -- Top --------------------------------myText:setReferencePoint( display.TopCenterReferencePoint ) -- myText.y = titleText.y + titleText.contentHeight + 10 scrollView:insert( myText ) sceneGroup:insert( scrollView ) local backBtn = display.newImageRect("images/Dublin/mountjoy.jpg", 360, 40) backBtn.x = display.contentCenterX backBtn.y = display.screenOriginY+20; backBtn.btnDestination = "dublin" backBtn:addEventListener( "touch", btnTouch ) sceneGroup:insert( backBtn ) local audioBtn = display.newImageRect("images/playAudioVisual.jpg", 360, 40) audioBtn.x = display.contentCenterX audioBtn.y = bottomSide -20 audioBtn:addEventListener( "touch", myAudio ) audioBtn:toFront( ) sceneGroup:insert( audioBtn ) arrowLeft = display.newImageRect("images/arrowLeftDark.png", 12, 21) arrowLeft.x = display.screenOriginX+20 arrowLeft.y = display.contentCenterY-80 arrowLeft:toFront( ) arrowLeft.id = "back" arrowLeft:addEventListener( "touch", handleButtonEvent ) sceneGroup:insert( arrowLeft ) arrowRight = display.newImageRect("images/arrowRightDark.png", 12, 21) arrowRight.x = display.contentWidth-display.screenOriginX -20; arrowRight.y = display.contentCenterY-80 arrowRight:toFront( ) arrowRight.id = "fwd" arrowRight:addEventListener( "touch", handleButtonEvent ) sceneGroup:insert( arrowRight ) local function moveToFront( ) arrowRight:toFront( ) arrowLeft:toFront( ) end end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if "did" == phase then print( "1: show event, phase did" ) local sceneName = composer.getSceneName( "previous" ) if sceneName ~= nil then composer.removeScene( sceneName, true ) end end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if "will" == phase then print( "1: hide event, phase will" ) -- cancel timer if isChannel1Playing == true then timer.cancel( changeTimer ); changeTimer = nil timer.cancel( changeTimer2 ); changeTimer2 = nil; end -- remove audio audio.stop() audio.dispose( audioSound ) end end function scene:destroy( event ) print( "((destroying scene 1's view))" ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene