How do I stop a Function?

Hi, I’m having a slight problem here. I’m using Director Class for my game menus, but when navigating to the game scene from the main menu, then back to the main menu, and then back to the game scene a second time, the game scene crashes. D=

I have narrowed down the problem though, the function for my scrolling background is causing the trouble (The same background scrolling function in the HorseAnimation demo, but with different assets). When I remove the function everything works fine while navigating between the game and the menus, but naturally, the backgrounds don’t move anymore.

Is there a way to stop or remove the function just before the Director changes the scene? The function is called “move”, and have tried ‘move = nil’ and ‘move:removeSelf()’. Is there a secret that I don’t know about? A BIG thanks in advance for any help given! [import]uid: 66329 topic_id: 19160 reply_id: 319160[/import]

I dont think you need to remove the function as long as you are not calling it when changing Scenes you should be fine. Make sure you Remove Runtimes and stop or cancel timers so the function no longer is being used if you are changing Scenes. Also if you would post some code it be a lot easier to identify your problem. As for your question I dont think you can “remove” a function but I could be wrong I never found a need to remove a function as long as is not being used when you dont need it.

Hope this helps. [import]uid: 30314 topic_id: 19160 reply_id: 73907[/import]

Ok, I’m using system.getTimer for the movement event, and I don’t think you can cancel that, but I might be wrong. I also removed the runtime, but it still didn’t work. Luckily for me though, I just made a work-around for my issue instead of using Runtime I made a timer that delayed the movement my a millisecond, and it works fine. Thank you for the help though.

[import]uid: 66329 topic_id: 19160 reply_id: 73911[/import]

Well, it would appear that I spoke a bit too soon. I’m still having issues with the scrolling background. Here’s the code that I’m using now [code]local function move(event)

local tDelta = event.time - tPrevious
tPrevious = event.time

------Speed and direction (changing the “xOffset” changes the speed of that layer)

local xOffset = ( 0.13 * tDelta )

moon.x = moon.x - xOffset*0.0

------houses

fog.x = fog.x - xOffset*2
fog2.x = fog2.x - xOffset*2

-----back mountain

mountain_big.x = mountain_big.x - xOffset*0.1
mountain_big2.x = mountain_big2.x - xOffset*0.1

----front mountain
mountain_sma.x = mountain_sma.x - xOffset*0.5
mountain_sma2.x = mountain_sma2.x - xOffset*0.5

-----upper trees
tree_s.x = tree_s.x - xOffset*0.6
tree_s2.x = tree_s2.x - xOffset*0.8
tree_s3.x = tree_s3.x ±xOffset*0.7
tree_s4.x = tree_s4.x - xOffset*0.8
tree_s5.x = tree_s5.x - xOffset*0.7
tree_s6.x = tree_s6.x - xOffset*0.8

-------chimmeny detectors
tree_D.x = tree_D.x - xOffset*2
tree_D2.x = tree_D2.x - xOffset*2
tree_D3.x = tree_D3.x - xOffset*2
tree_D4.x = tree_D4.x - xOffset*2
tree_D5.x = tree_D5.x - xOffset*2
tree_D6.x = tree_D6.x - xOffset*2

----lower trees

tree_l.x = tree_l.x - xOffset*1
tree_l2.x = tree_l2.x - xOffset*1.2
tree_l3.x = tree_l3.x - xOffset*1.2

------Forground trees (now transparent)
tree_l_sugi.x = tree_l_sugi.x - xOffset * 1.5
tree_l_take.x = tree_l_take.x - xOffset * 1.5

—Seamless Linking (change the values so they match up with your image size)
-----moon

if moon.x < -480 + moon.width / 2 then
moon:translate ( 480*2 , 0)

-------houses

end
if fog.x < -1920 + fog.width / 2 then
fog:translate( 1920 * 2, 0)
end

if fog2.x < -1920 + fog2.width / 2 then
fog2:translate( 1920 * 2, 0)
end
------back mountain

if mountain_big.x < -480 + mountain_big.width / 2 then
mountain_big:translate(480*2 , 0)
end
if mountain_big2.x < -480 + mountain_big2.width / 2 then
mountain_big2:translate(480*2 , 0)
end

------front mountain
if mountain_sma.x < -480 + mountain_sma.width / 2 then
mountain_sma:translate(480*2,0)
end
if mountain_sma2.x < -480 + mountain_sma2.width / 2 then
mountain_sma2:translate(480*2,0)
end

-------upper trees

if tree_s.x < -480 + tree_s.width / 2 then
tree_s:translate(480*2 , 0)
end
if tree_s2.x < -480 + tree_s2.width / 2 then
tree_s2:translate(480*2 , 0)

end
if tree_s3.x < -480 + tree_s3.width / 2 then
tree_s3:translate(480*2 , 0)
end
if tree_s4.x < -480 + tree_s4.width / 2 then
tree_s4:translate(480*2 , 0)
end
if tree_s5.x < -480 + tree_s5.width / 2 then
tree_s5:translate(480*2 , 0)
end
if tree_s6.x < -480 + tree_s6.width / 2 then
tree_s6:translate(480*2 , 0)
end

---------Chimmeny detectors
if tree_D.x < -960 + tree_D.width / 2 then
tree_D:translate(960*2 , 0)
end

if tree_D2.x < -960 + tree_D2.width / 2 then
tree_D2:translate(960*2 , 0)
end

if tree_D3.x < -960 + tree_D3.width / 2 then
tree_D3:translate(960*2 , 0)
end

if tree_D4.x < -960 + tree_D4.width / 2 then
tree_D4:translate(960*2 , 0)
end

if tree_D5.x < -960 + tree_D5.width / 2 then
tree_D5:translate(960*2 , 0)
end

if tree_D6.x < -960 + tree_D6.width / 2 then
tree_D6:translate(960*2 , 0)
end
-------lower trees

if tree_l.x < -480 + tree_l.width / 2 then
tree_l:translate(480*2 , 0)
end

if tree_l2.x < -480 + tree_l2.width / 2 then
tree_l2:translate(480*2 , 0)
end

if tree_l3.x < -480 + tree_l3.width / 2 then
tree_l3:translate(480*2 , 0)
end

--------forground trees (now transparent)

if tree_l_sugi.x < -480 + tree_l_sugi.width / 2 then
tree_l_sugi:translate(480*4,0)
end

if tree_l_take.x < -480 + tree_l_take.width / 2 then
tree_l_take:translate(480*5,0)
end

end
local function moveTmr (event)
timer.performWithDelay( 1, move )
end
Runtime:addEventListener(“enterFrame”,moveTmr) [import]uid: 66329 topic_id: 19160 reply_id: 73928[/import]

You didn’t mention if this is on the device or the simulator. If it’s on the device you may be loading more textures than what the device allows. If this is the case you should monitor the texture memory usage to see if something fishy is going on.

BTW, why is there a 1ms delay on the move function? [import]uid: 61899 topic_id: 19160 reply_id: 73953[/import]

Hi there CluelessIdeas! I have just resolved the problem! It was the Runtimes. I was calling Runtime:removeEventListener(“enterFrame”,move) to remove the Runtimes, but the move function was below Runtime:removeEventListener. It was all a matter code placement. Thanks for all of the great help though. I have definitely learned a lesson from this. [import]uid: 66329 topic_id: 19160 reply_id: 74019[/import]