I'm having an issue with detecting all the objects in a table for a specific collision. I can only get the score to increase when the first object in the table is hit with snowball. Here's the code I'm using for the object's creation and collision. I have a function call for createSnowBall() after button push. That part works fine.
local physics = require("physics")
physics.start()
physics.setGravity( 0, 0 )
local snowBalls = {}
local ornaments = {}
local score = 0
local scoreText = display.newText( "Score: " .. score, 70, 25, native.systemBoldFont, 32 )
function createSnowBall()
snowBall = display.newImageRect( "snowball.png", 20, 20)
snowBall.x = gun.x
snowBall.y = HEIGHT - 110
physics.addBody( snowBall, { density = 1.0, friction = 1, bounce = 0, radius = 20 } )
snowBall.isSnowBall = true
snowBalls[#snowBalls+1] = snowBall
moveSnowBall(snowBall)
return snowBall
end
function createOrnament(num)
if num == 1 then
ornament = display.newImageRect( "blueO.png", 30, 40)
elseif num == 2 then
ornament = display.newImageRect( "redO.png", 30, 40)
elseif num == 3 then
ornament = display.newImageRect( "greenO.png", 30, 40)
end
ornament.isOrnament = true
ornaments[#ornaments+1] = ornament
ornament.x = math.random(50, 270)
ornament.y = 3
local radius = 15
physics.addBody( ornament, { density = 1.0, friction = 1, bounce = 1, radius = radius } )
ornament:applyForce(35, 70, ornament.x + 4, ornament.y + 4)
return ornament
end
createOrnament(math.random(1, 3))
function snowBallCollision(event)
if event.phase == "began" then
local target = event.other
if target.isSnowBall then
score = score + 5
scoreText.text = "Score: " .. score
end
end
end
ornament:addEventListener( "collision", snowBallCollision )