It’s not the best method, but it’ll get you started.
- give each object you create a name so you can later uniquely identify it (Yes, there are better ways of doing this).
- look for that name in the collision event and do stuff
Example, when a Player’s bullet hit’s a bad guy. This code is inside the bad guy (here is the full class).
[lua]function onHit(self, event)
– TODO/FIXME: string names? Really? That’s great man…
if(event.other.name == “Bullet”) then
– TODO: create enemy death
–createEnemyDeath(self.x, self.y)
–print("ship is dead, ID: ", self.ID)
event.other:destroy()
elseif(event.other.name == “Player”) then
event.other:onBulletHit()
end
local smallShipDeathSoundChannel = audio.play(EnemySmallShip.smallShipDeathSound)
audio.setVolume(.4, {channel = smallShipDeathSoundChannel})
self:destroy()
end
img.collision = onHit
img:addEventListener(“collision”, img)[/lua]
img is the image of the bad guy. He’s been added to the physics via addBody. Notice the onHit collision code. It basically says “If the thing I hit is called ‘Bullet’, then I’m dead bro! Otherwise, if I hit the ‘Player’ then call his onBulletHit method. Either way, I hit something, explode and die and play the sound.”
You dig? [import]uid: 23693 topic_id: 14708 reply_id: 54796[/import]