collision problem

Hi,

now I have built lots of games and published them so I think i know what I am doing but I am totally stuck with this, I really cannot see what I am wrong.

My collision handling isnt handling the carTop and bottom Collision. Well the carTop is colliding with the bottomrect which is a static object, but it inst removing the target i.e. the carTop. I know the condition isnt firing.

If I swap the elseif statement and ask if the phase has “began” it will work, but when I collide with other cars I always get a runtime error that the target in bottomrect/topcar condtion dosnt exist…pulling my hair out :slight_smile:

here is the collision function.

[lua]

 function carTop:collision(event)
                local phase = event.phase
                local target = event.target
                local other = event.other

                if phase == “began” then
                    if target.name == “carTop” and other.name == “carLeft” or
                            target.name == “carTop” and other.name == “carRight” or                        
                            target.name == “carTop” and other.name == “fireEngine2” or
                            target.name == “carTop” and other.name == “fireEngine3”
                    then
                        lives = lives - 1
                        display.remove(target)
                        target = nil

                        if lives < 1 then
                            physics.pause()
                            timer.performWithDelay(1500, gameOver)
                        end
                    end
                elseif target.name == “carTop” and other.name == “bottomRect” then
                    display.remove(target)
                    target = nil

                    scoreNumber = scoreNumber + 10
                    updateScore()
                end
            end

[/lua]

Looks to me like the elseif condition is screwy…

if phase == “began” is OK,  but I’d expect the else if to be: elseif phase==“ended” not some other unrelated condition

Then you can work out which collision phase the test for carTop and bottomRect belongs in.

Take a look here for a good model of a touch listener. There is admittedly a lot you could remove for your case, but the general if statement structure is the way to go. Also, this listing is good for when you need to drag things around.

Looks to me like the elseif condition is screwy…

if phase == “began” is OK,  but I’d expect the else if to be: elseif phase==“ended” not some other unrelated condition

Then you can work out which collision phase the test for carTop and bottomRect belongs in.

Take a look here for a good model of a touch listener. There is admittedly a lot you could remove for your case, but the general if statement structure is the way to go. Also, this listing is good for when you need to drag things around.