Adding collision listeners to all objects in a table

I’m currently trying to replicate “Swipe Brick Breaker” a clone of one of the games made by 111% games.

 

 

https://www.youtube.com/watch?v=NrjuN6Vx7KM <------ (this is what I’m trying to make)

 

I currently have 3 balls moving on screen but only the first ball spawned can properly collide with the bricks. The balls are being iterated through a ballTable within the enterFrame listener so that they can all move. I thought I would do the same thing for collision ie: have collision function iterate through the ballTable so that all balls can collide with my brick but this somehow only works for one ball. Any help would be greatly appreciated  

 

Here’s what I have:

 

[spoiler]

 -- ball properties function updatenewbullet() for key,ball in pairs(ballTable) do if ball ~= nil then -- Movement ball.x = ball.x + ball.velocityX ball.y = ball.y + ball.velocityY -- If ball hits the ceiling or left or right wall, bounce off of it if ball.x \< 0 or ball.x + ball.width \> display.contentWidth then ball.velocityX = -ball.velocityX end -- Bounce off top wall if ball.y \< -30 then ball.velocityY = -ball.velocityY end -- stop if ball reaches bottem wall if ball.y \> display.contentHeight + 20 then ball.velocityY = 0 ball.velocityX = 0 ballTable[key] = nil -- CLEAR BALL FROM TABLE display.remove( ball ) -- remove ball end end end end function spawnnewballs() local ball = display.newCircle( 100, 100, 5 ) ball.name = "ball" ball.collision = ballsCollision ball:addEventListener( "collision" ) ball.x = startx ball.y = starty ball.velocityX = newballvelocityX ball.velocityY = newballvelocityY physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 0 }) ball.colliderName = "ball" ballGroup.insert(ballGroup, ball) table.insert(ballTable, ball) end function ballsCollision(self, event) local other = event.other if( other.colliderName ~= "brick" ) then return false end for key,ball in pairs(ballTable) do if ( event.phase == "began" ) then if ball ~= nil then if ball.y + ball.height \* 0.5 \< event.other.y + event.other.height \* 0.5 then if hitchecky == true then ball.velocityY = - ball.velocityY hitchecky = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitchecky ) end end if ball.x - ball.width \* 0.5 \> event.other.x - event.other.width \* 0.5 then if hitcheckx == true then ball.velocityX = - ball.velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitcheckx ) end end if ball.x + ball.width \* 0.5 \< event.other.x + event.other.width \* 0.5 then if hitcheckx == true then ball.velocityX = - ball.velocityX hitcheckx = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitcheckx ) end end if bullet.y - bullet.height \* 0.5 \> event.other.y - event.other.height \* 0.5 then if hitchecky == true then ball.velocityY = - ball.velocityY hitchecky = false --event.other:removeSelf() bricklocationx = event.other.x / 43 bricklocationy = event.other.y / 30 spawngrid[bricklocationx][bricklocationy] = 0 timer.performWithDelay( 20, changehitchecky ) end end end end end end

[/spoiler]

sorry didnt read the code, but from my understanding here’s how you can do it.

local myBricks = {} myBricks[1] = display.newRect(arguements) myBrick[1].tag = "brick" physics.addBody(myBricks[1], "static", {friction=0.5}) --sample bricks, put tag to identify local myBalls = {} --create balls where x=num of balls for i=1,x,1 do myBalls[i] = display.newCircle(0,0,10) physics.addBody(myBalls[i]," dynamic",{radius=10}) myBalls[i].collision = function(self, event) if event.others.tag == "brick" then --put code when ball collide with brick end end myBalls[i]:addEventListener("collision") end

it will check is myBall[i] collided with other object with tag of “brick”

hope this help,(not tested since im on mobile)

Try replacing:

for key,ball in pairs(ballTable) end

To:

for i = 1, #ballTable end

FYI, this seems more like a situation where “global” collision handling should be used. In many-to-many situations like this… multiple balls and multiple bricks… that usually makes it easier (and more efficient) than adding unique listeners to a ton of unique objects.

Brent

sorry didnt read the code, but from my understanding here’s how you can do it.

local myBricks = {} myBricks[1] = display.newRect(arguements) myBrick[1].tag = "brick" physics.addBody(myBricks[1], "static", {friction=0.5}) --sample bricks, put tag to identify local myBalls = {} --create balls where x=num of balls for i=1,x,1 do myBalls[i] = display.newCircle(0,0,10) physics.addBody(myBalls[i]," dynamic",{radius=10}) myBalls[i].collision = function(self, event) if event.others.tag == "brick" then --put code when ball collide with brick end end myBalls[i]:addEventListener("collision") end

it will check is myBall[i] collided with other object with tag of “brick”

hope this help,(not tested since im on mobile)

Try replacing:

for key,ball in pairs(ballTable) end

To:

for i = 1, #ballTable end

FYI, this seems more like a situation where “global” collision handling should be used. In many-to-many situations like this… multiple balls and multiple bricks… that usually makes it easier (and more efficient) than adding unique listeners to a ton of unique objects.

Brent