How do you end/stop calling a function?

HI all! I can’t figure out how to end/stop calling a function.
So here’s an example
[lua]–spawns a function, adds physics to the object, adds a velocity function
function sp ()
for s = 1, 1 do
p = display.newImage(“platform_1.png”);
p.y = 690
physics.addBody( p, “kinematic”,{density = 1, friction = 0.3, bounce = 0.0} )
p.isFixedRotation = true
– moves the image upwards
Velocity ()
end
end
end
–starts the spawning process
local spawnImage = timer.performWithDelay( 5000, sp, 0 )

– velocity function/sets speed of p
function Velocity ()
p:setLinearVelocity( 0, -200 )
end [/lua]
I want this function to go on forever unless an event is triggered.
And I want all the images that the function creates to remove themselves once they move off the screen.
so how do you end these functions/stop calling them and how do you remove the images that it’s creating after they move off the screen?
thanks in advance!
[import]uid: 70979 topic_id: 12619 reply_id: 312619[/import]

Hi.

My post here:

http://developer.anscamobile.com/forum/2011/07/18/functions-transitions-and-physics

contains some code which spawns objects based on a timer then kills them once a transition is completed. Take a look at that and see if it fits your needs.

In terms of stopping calling a function, I’m still very new to Lua but there are a number of ways.

you could use: timer.cancel(name-of-your-timer) to just turn it off.

If you’re looking for something more fancy, you could use a variable in the millisecond paramater of the timer, and use an argument/function to change this value, depending on your needs.

Is that the kind of thing you’re looking for? [import]uid: 67933 topic_id: 12619 reply_id: 46178[/import]

I have just deleted the duplicate thread you made of this. Make ONE thread only per topic.

Please read the rules before posting again; http://developer.anscamobile.com/forum/2011/05/05/forum-rules-and-guidelines

Also note that you need to put your code in Lua tags by using < lua > and < / lua > without the spaces.

Peach Pellen [import]uid: 52491 topic_id: 12619 reply_id: 46226[/import]

Thanks spider_newgent for responding!

My question about how to end/stop calling a function, well I want this function to end when I switch screens with director.
I’m really new to director and I don’t know how to use it well.
I’m currently stuck on trying to clean the old screen of moving objects when I switch to a new one. If you have any tips on doing this with director then please share.
[import]uid: 70979 topic_id: 12619 reply_id: 46553[/import]

I Haven’t started playing with director yet, so sorry, but I wont be able to help you.

Try posting your code, and I’ll take a look, but you’re probably better off searching the director threads.

Good luck! [import]uid: 67933 topic_id: 12619 reply_id: 46627[/import]

Hi.
before you change scene in director use:
[lua]timer.cancel (spawnImage) [/lua] [import]uid: 13632 topic_id: 12619 reply_id: 46630[/import]

Thanks guys I finaly figured it out!:wink:
I guess I skiped something really simple.
I figured out that when a function is calling a function and they keep calling each other(kind of like a loop) then you have to set them to nil to stop them.

Thanks again! [import]uid: 70979 topic_id: 12619 reply_id: 46824[/import]

Hi

Did you figure out how to remove the images once they go off screen?

I’ve tried using Object:removeSelf, and Object = nil.

The problem is that when the function is next called it gives me the following runtime error:

Attempt to index upvalue ‘crater’ (a nil value).

Thanks [import]uid: 67933 topic_id: 12619 reply_id: 46976[/import]

I found a post that shows how to remove Images after they disappear off the screen.
I wasn’t able/I don’t think I can make this work with my function, but I think it will work with yours.
Follow this link and let me know how it goes.
http://developer.anscamobile.com/forum/2011/07/19/how-do-you-endstop-calling-function#comment-form

Fun Times!
[import]uid: 70979 topic_id: 12619 reply_id: 47080[/import]