Event Listener is spawning a lot of objects! Help!

I’ve created an Event Listener to spawn a ball when the screen is touched.
However, when ever I “touch” (click) the screen on the simulator, many balls are spawned. A new ball is spawned every time you move the mouse, and two times when you click. It’s honestly quite bizarre.
[lua]function spawnBall()
ball = display.newImage(“ball.png”)
ball.x = 180
ball.y = 80
physics.addBody( ball, { density=2.0, friction=0.5, bounce=0.3 } )
end

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

Thanks in advance. I’d really like to get this problem solved. [import]uid: 25216 topic_id: 14064 reply_id: 314064[/import]

Try this (haven’t tried it myself so not sure if it works):

[lua]function spawnBall(event)
if event.phase == “began” then
ball = display.newImage(“ball.png”)
ball.x = 180
ball.y = 80
physics.addBody( ball, { density=2.0, friction=0.5, bounce=0.3 } )
end
end

Runtime:addEventListener( “touch”, spawnBall ) [/lua] [import]uid: 19383 topic_id: 14064 reply_id: 51791[/import]

@ codepunk_schmidt is right. the touch event listener is triggered many times for a single touch. but the began phase is triggered only once.

alternatively you can try using “tap” event.
[lua]function spawnBall()
ball = display.newImage(“ball.png”)
ball.x = 180
ball.y = 80
physics.addBody( ball, { density=2.0, friction=0.5, bounce=0.3 } )
end

Runtime:addEventListener( “tap”, spawnBall )[/lua] [import]uid: 71210 topic_id: 14064 reply_id: 51828[/import]