Help with a function!

Hi. I made a function, which spawns a ball.

[lua]function spawnBall()
local ball = display.newImage( “ball.png” )
physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.0, radius = 100 } )
physics.setGravity( 0, 10 )
crate.x = math.random(250)
crate.y = -40
end[/lua]

In my project, I want there to be a Button, that spawns the ball. I’ve got this figured out, accept I only want ONE ball to spawn. Is there a way to limit the number of times a function can run? This would help me in many ways.

Here’s the button code:
[lua]local play = display.newImage( “play.png” )
play:scale( .5, .5 )
play:addEventListener( “touch”, spawnBall )[/lua] [import]uid: 25216 topic_id: 11712 reply_id: 311712[/import]

This line:

[lua]play:addEventListener( “touch”, spawnBall )[/lua]

…kicks off at least two events, so to narrow that down you can only handle the event on one of them, like this:

[lua]function spawnBall(e)
if e.phase == “ended” then
– do stuff here
end
end[/lua]

That will keep a ball from spawning multiple times on one tap of the button.

If you want one ball and one ball only, you could remove the event listener inside the spawnBall() function and that way it will never happen again.

[lua]function spawnBall(e)
if e.phase == “ended” then
– do stuff here
play:removeEventListener(“touch”, spawnBall)
end
end[/lua]

Jay [import]uid: 9440 topic_id: 11712 reply_id: 42581[/import]

I’m confused. The code didn’t work. It just made the “play” button go away.
Could you elaborate a little bit?

[lua]function spawnBall(e)
if e.phase == “ended” then
function spawnBall()
local ball = display.newImage( “ball.png” )
physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.0, radius = 100 } )
physics.setGravity( 0, 10 )
ball.x = contentWidth/2
ball.y = 0
end
local play = display.newImage( “play.png” )
play:scale( .5, .5 )
play:removeEventListener(“touch”, spawnBall)
end
end
[lua] [import]uid: 25216 topic_id: 11712 reply_id: 42583[/import]

[lua]function spawnBall(e)
if e.phase == “ended” then
local ball = display.newImage( “ball.png” )
physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.0, radius = 100 } )
physics.setGravity( 0, 10 )
ball.x = contentWidth/2
ball.y = 0
play:removeEventListener(“touch”, spawnBall)
end
end

local play = display.newImage( “play.png” )
play:scale( .5, .5 )
play:addEventListener(“touch”, spawnBall)[/lua]

That should do it (although I didn’t run the code).

Jay [import]uid: 9440 topic_id: 11712 reply_id: 42586[/import]

Yup! It worked. :smiley: Now I just need to make a function that stops the button.

**researches** [import]uid: 25216 topic_id: 11712 reply_id: 42588[/import]

Thanks! Your really awesome for helping me. :slight_smile: [import]uid: 25216 topic_id: 11712 reply_id: 42590[/import]

No problem, I enjoyed it. :slight_smile:

I’d just got done doing a new build of my other program and needed a “Corona” break.

Jay
[import]uid: 9440 topic_id: 11712 reply_id: 42599[/import]

I’d just got done doing a new build of my other program and needed a “Corona” break.

Couldn’t resist saying - in my case all I am doing, I think, is taking Corona breaks. I need to take break from breaks to get my program completed. lol… [import]uid: 48521 topic_id: 11712 reply_id: 42611[/import]