DOUBLING objects, HELP!

Hello, there is a simple code which turn on a function for creating objects. Why does it makes 4 objects instead 2??? I tried other way with tables but result is same!

function createMultipleBalls(num)

for i=1, num do

shotBall = display.newImage(“shot_ball.png”)

shotBall.xScale = 0.7

shotBall.yScale = 0.7

shotBall.x = math.random(50,500)

shotBall.y = math.random(50,500)

shotBall:addEventListener(“touch”, dragBalls)

end

end

function touchScreen(event)

ki=2

if event.phase == “ended” then

–[[mBall:removeSelf()

createBall()–]]

Runtime:removeEventListener(“enterFrame”, fadeInReady)

mBall.enterFrame = bobUp

Runtime:addEventListener(“enterFrame”, mBall)

createMultipleBalls(2)

stopReady()

end

end

I’m going to take a guess here and say that:

  1. You’re putting your touch listener on the Runtime.

  2. You never return true from your touch listener saying you’ve handled touch.

As for #1 I just helped another developer with this. Basically you shouldn’t put a touch handler on Runtime.  Yes you can, but you shouldn’t.  Instead create a full screen rectangle, make it invisible and hit testable and add your touch handler to it:

local fullScreenButton = display.newRect(display.contentCenterX, display.contentCenterY, display.acutalContentWidth, display.actualContentHeight) (add to a group if needed) fullScreenButton.isVisible = false fullScreenButton.isHitTestable = true fullScreenButton:addEventListener( "touch", touchScreen )

If you have other touchable items on the screen, they need to be layered above this one or they won’t get their touch events.

Rob

Okay, Rob, I’ll try it thnx. But i noticed 1 thing - if I get out Listener from “function scene:show()” it’s going work correct.

Your listener functions should be written in the scene’s main chunk above the scene:create() function. If you have variables you need access to in the listener function that you’ve declared local inside of one of the scene functions, simply forward declare the variable at the top.

I’m going to take a guess here and say that:

  1. You’re putting your touch listener on the Runtime.

  2. You never return true from your touch listener saying you’ve handled touch.

As for #1 I just helped another developer with this. Basically you shouldn’t put a touch handler on Runtime.  Yes you can, but you shouldn’t.  Instead create a full screen rectangle, make it invisible and hit testable and add your touch handler to it:

local fullScreenButton = display.newRect(display.contentCenterX, display.contentCenterY, display.acutalContentWidth, display.actualContentHeight) (add to a group if needed) fullScreenButton.isVisible = false fullScreenButton.isHitTestable = true fullScreenButton:addEventListener( "touch", touchScreen )

If you have other touchable items on the screen, they need to be layered above this one or they won’t get their touch events.

Rob

Okay, Rob, I’ll try it thnx. But i noticed 1 thing - if I get out Listener from “function scene:show()” it’s going work correct.

Your listener functions should be written in the scene’s main chunk above the scene:create() function. If you have variables you need access to in the listener function that you’ve declared local inside of one of the scene functions, simply forward declare the variable at the top.