[Resolved] Collision issues.

Hello everyone, I have an interesting bug. The game is setup with hands at the bottom. When the screen is touched the hands throw a Chinese star at a target moving along the x-axis at top of the screen. As the player progresses the targets begin to move faster. The old targets that were already on the screen stay at their initial speed. They begin to overlap. When this occurs if the star hits two targets at the same time it causes a bug freezes the game. Now obviously the targets and stars are removed upon collision. Here’s the code.

[lua] local function spawnTarget1( event )
target1 = display.newImage(“target_final.png”)
target1.name = “target1”
physics.addBody(target1, “kinematic”, {density=0.0, friction=0.0, bounce=0.0, radius= 15})
target1:setReferencePoint(display.CenterReferencePoint)
target1.y = 65
if gamePhase == “Phase one” then
transition.to(target1, {time = 30000, x = display.contentWidth + 200})
elseif gamePhase == “Phase three” then
transition.to(target1, {time = 10000, x = display.contentWidth + 200})
elseif gamePhase == “Phase four” then
transition.to(target1, {time = 9000, x = display.contentWidth + 200})
elseif gamePhase == “Phase five” then
transition.to(target1, {time = 8000, x = display.contentWidth + 200})
elseif gamePhase == “Phase six” then
transition.to(target1, {time = 7000, x = display.contentWidth + 200})
end

if target1.x > 590 then
table.insert(toRemove2, event.other)
end
mg:insert(target1)
return target1
end

local function onCollision(self, event)

if self.name == “star” and event.other.name == “target1” and gamePhase ~= “Phase over” then
score = score + 1
scoreText.text = score
audio.play(sounds.hit)
table.insert(toRemove, event.other)
table.insert(toRemove2, self)
phaseUpdate()

elseif self.name == “star” and event.other.name == “statue” and gamePhase ~= “Phase over” then
gameEnd()
end

end
[import]uid: 49863 topic_id: 28734 reply_id: 328734[/import]

When you run in the simulator and the game freezes, are there any messages in the terminal window? Usually this will point you to the line number in your code that is causing the issue.

One possibility is that you are attempting to remove the star twice, but since it was already removed after the first collision, attempting to remove it again would cause a crash (unless you check beforehand whether it exists).

  • Andrew [import]uid: 109711 topic_id: 28734 reply_id: 115867[/import]

One reason this can happen is trying to remove or manipulate an object before the collision is resolved; you may want to try setting event.phase == “ended” in your collision function. [import]uid: 52491 topic_id: 28734 reply_id: 115891[/import]

Thank you aukStudios and Peach. Peach your solution worked great and seems to have fixed the bug. [import]uid: 49863 topic_id: 28734 reply_id: 115943[/import]

Fabulous to hear that; I’ve had this issue a few times myself and it usually seems to do the trick :wink:

Marking as resolved :slight_smile: [import]uid: 52491 topic_id: 28734 reply_id: 116042[/import]