Quick question to do with collisions

Can collisions have specific outcomes depending on the type of object your player is colliding with? My collision function is applied to my player, so I don't know how you can differentiate what type of enemy is hitting it:

1
2
3
4
local function onPlayerCollision()
print("collided with enemy")
end
player:addEventListener("collision",onPlayerCollision)

you can attach event listener to specific object and make it respond to specific other object
if you want different things for different "other objects" just stack some elseif in there

something like that, i used it in my code and it works

1
2
3
4
5
6
7
8
9
local function onCollision(event)
        if event.other.name == "enemy" then
                event.other:removeSelf()
                highScore = highScore + event.other.health
                scoreField.text = highScore
                event.target.isVisible = false
                event.target:removeEventListener("collision",onCollision)
        end
end

Hi dark consoles,

Thanks so much for the suggestion. I'm sure it works.

In my case though, I'm having trouble, I'm guessing because my "enemy" is hidden within a function:

1
2
3
4
5
6
7
local function scooterSpawn (event)  
  local scooter = display.newRect(20,20,20,20)
  scooter.x = -100
  scooter.y = math.random(120,924)
  transitions[#transitions + 1] = transition.to(scooter,  {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
  spawnedGroup:insert(scooter)
end

you have to declare scooter.name == "scooter" first, then attach listener to scooter inside spawning function

Awesome, that did the trick! You're a star darkconsoles, thank you so much.

Cheers,
Steven

transitions[#transitions + 1] = transition.to

i cant understand why did you use this line, i always store my objects in table and then issue transitions on it

That's a good question, I've been wondering that myself. That came from a code snippet put forward by another user in a previous thread. I'll find it, and send you the link..

This is it here: https://developer.anscamobile.com/forum/2011/09/19/how-do-you-manipulate-spawned-objects
It's a long one, if you figure it out please let me know. Now that I'm taking a second look, I'm starting to think that line might not even be necessary.

Cheers,
Steven

Yup, it wasn't necessary. I just chopped out the transitions[#transitions + 1] =
from my code and everything runs as normal.

I guess I should thank you one more time for helping me come to that realization (embarrassed face). Once again darkconsoles, a very sincere: "thank you".

Cheers,
Steven

any time, happy to help

views:1534 update:2011/10/25 9:10:48
corona forums © 2003-2011