Collision not Working! Please help Bullet - Enemy!

This code detects the collision how do i make it so it only does something when the bullet hits it event.other.type == bullet???

local function onLocalCollision(e) print("COLLISON") end soldier:addEventListener( "collision", onLocalCollision )

That isn’t the correct usage of the timer API. See here:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

The timer must be inside your collision function, and it must refer to (call) the function above it. You can write “inline” timer functions too, but don’t worry about that now, just work on solving and understanding the physics.

[lua]

function removeBody()

  soldier:removeSelf()

  soldier = nil

  print(“DEAD”)

end

function onLocalCollision( self, event )

  if event.other.name == “bullet” then 

    display.remove(event.other.name)

    soldierHealth = soldierHealth - 5

    if soldierHealth == 0 then

      timer.performWithDelay( 10, removeBody )

    end

  end

end

[/lua]

Hi @krpatel420,

It appears that you’re mixing up the “local” vs. “global” collision styles. If you want to use the local method, the proper format is this:

[lua]

local function onLocalCollision( self, event )

  --solider is “self”

  --other object is “event.other”

end

soldier.collision = onLocalCollision

soldier:addEventListener( “collision”, soldier )

[/lua]

Notice, especially, how you need to add the “.collision” property to the solider, which points to the collision handling function. The name of the function does not go inside the “addEventListener” line in the case of a local collision listener.

Brent

local function onLocalCollision( self, event ) if event.other.name == "bullet" then soldierHealth = soldierHealth - 5 if soldierHealth == 0 then print("Dead!") end end end soldier.collision = onLocalCollision soldier:addEventListener( "collision", soldier )

This seems to work to detect the collision now i can’t seem to get the health to work correctly when do i put in the variable soldierHealth = 10. Right now it is in the spawnSoldier() function  and in the game.lua but neither seem to be the right place. Thanks for your help so far I was getting extremely frustrated!

EDIT: Ohh I see because the function is local so now i have to make it global so the values are shared correct?

EDIT2: Now that i have changed the code the listener is thinking the bullet collided with the bullet even though i haven’t event fired it yet… I wonder why. Also when i make it display the e.target.name it says nil, but the bullet works correctly.

Thank you! It’s working correctly now! All i have to do is get the soldier to dissapear once his health == 0, for some reason he stays though. i am using display.remove(e.target) also tried display.remove(soldier)

EDIT: It works with the bullet, not the soldier, is it because it is a sprite sheet being spawned in or that there are multiple of them?

Hi @krpatel420,

What is your “common” physics object? Is there one soldier and many bullets? Or one bullet shooting down many soldiers? In general, you should try to put collision listeners on the least amount of objects.

For example, if you have one bullet and 50 soldiers, you should put the collision listener on the bullet and the soldiers (or anything the bullet hits) becomes the “event.other” object in the collision. In contrast, if you have 1 soldier and 50 bullets, the collision listener should be placed on the soldier, and then the bullets become the “event.other” in the collision while the soldier remains the “self” object.

If you have both many bullets AND many soldiers, you might want to consider a “global” collision listener instead.

Again, remember, to remove (or just reference) the objects in a local-style collision, “self” is the object that has the listener applied, and “event.other” is an object which it collides with.

Brent

They are both being spawned in in total you have 21 bullets(unless you upgrade) and 1 +3(wave #) soldiers and other unplanned “enemies” coming on different waves. It is now detecting the collisions but i can’t get the sprite/soldier to get off the screen, the bullet does though. i am using display.remove(e.target) also tried display.remove(soldier). for the bullet display.remove(bullet) works perfectly

In the collision handler, as I mentioned, the objects involved are “self” and “event.other” (at least for a local-style collision… it’s different for a global-style Runtime collision listener, as shown in the documentation).

So, to remove the soldier in the example, it’s:

[lua]

display.remove( self ) ; self = nil

[/lua]

BUT, this doesn’t entirely clear the object from memory. You need to “nil” its reference if you have the solider referenced somewhere outside the scope of the spawn function. If you don’t, Lua will not consider this object “garbage” and it will remain in memory, causing a memory leak.

Brent

The above code still hasn’t worked… it does print DEAD but nothing happens the the actual soldier on the simulator.

function onCollision(e) if e.phase == "began" then if(e.other.name == "bullet") then display.remove(e.other) soldierHealth = soldierHealth - 5 print(soldierHealth) if soldierHealth == 0 then display.remove( self ) ; self = nil print("DEAD") end end end end

Now it looks like you’ve switched to the “global-style” collision listener. That’s OK, but “self” is not a parameter in that usage. Instead, it’s “event.object1” and “event.object2”.

Review the documentation carefully on this:

http://developer.coronalabs.com/content/game-edition-collision-detection

If I may ask, is this your first game in Corona? You might find some game tutorials very useful. There are two video tutorials on YouTube that I can think of:

youtube.com/watch?v=Mw-TWx3CH84

youtube.com/watch?v=0GtUxdSeWzk

Hope this helps,

Brent

It actually isn’t this is my second but my first was so uber simple it wasn’t even funny, it had 4 screens, and the gameplay was running away from two “enemies” using transition.to. Sorry if i seem like a noob at this I’m just 14 trying to make some apps it is really fun, but i don’t think i really get the Collision parts of it correctly, the rest i understand!

That’s fine, we have other young developers using Corona. I think those video series should really help though… they explain some physics-related issues in pretty good detail. And when you need more help with physics stuff, just ask… myself and others here in the forums can provide assistance on that.

Brent

Okay so i was changing it and everything, and now the images print DEAD and they do not interact, they are still visible though, and the bullets still hit them, I did soldier:removeSelf() soldier = nil, i also tried soldier.isVisible = false. Like the tutorial you showed me

EDIT: Okay so now something different is happening, the bullets now just pile up infront of the moving line of soldiers, meaning the soldiers are still phyiscal bodies, they won’t change nor leave the screen.

Hi @krpatel420,

Have you studied the sample Physics projects in the Corona application? That code might help you see how to set up collisions properly. On your computer, they’re here:

Corona SDK > SampleCode > Physics

Yes, that is the first place i tried. I have been trying to fix this days before i posted on here using this as a last resort, trying not to make someone else do all the work. I’m sorry if that’s happening, but I’m stumped the collision detection stops, but the sprites keep moving,and they still act like a physics body.

I moved the function into the .lua that is made to spawn the soldiers, and now only the soldier spawned in the back disappears while the other four continue moving. Do they each have to be individuals?

EDIT: So now there is progress, the back soldier gets removed but the other 3 keep moving. (This will be the outline for the rest of the monsters i will create for the game so this will not be a problem for the other times)

I think the main thing to establish is the references to objects. If you have multiple soldiers and multiple bullets, “soldier” doesn’t refer to all of the soldiers. Even if you assign the last-created solider to that handle, the other soldiers won’t equate to that. So, you can’t just do “soldier:removeSelf()” on the collision.

That’s why it’s important to use the “event” references from the collision to determine exactly which two objects (which bullet and which soldier) you want to deal with. So, depending on the style of collision you use (local style or global style), those parameters will vary in code… but if you get them correct, you shouldn’t have any problem getting rid of them on screen.

Brent

I guess the true problem is getting them correct, looks like I will stay up a bit longer to research this stuff, it really gets confusing when you add multiple of one thing. In my game Run there were just two enemies and one you, they were each individual images and all. And after a collision you just had to change scenes.

It’s not nearly as complicated as this. Having to take away health, get rid of objects. I don’t think i quite understand the collision references even after reading all the documentation so many times. Anyway i’l keep trying and get back to you tomorrow morning (it’s 10:00 here). Thanks for helping me so much Brent! 

So I have been expiramenting for days, but the same error keeps coming up event.object1 is a nil value, i followed the tutorial on the corona website exactly, and now i don’t understand what’s happening

After all the progress i have made, all that happens now is the soldiers are not visible but their physics bodies are, the bullets don’t remove themselves anymore because of them, they do get pushed back though

EDIT: I set the draw mode for physics to hybrid and i found out that the physics body remains after the image is deleted, so i tried physics.removeBody(soldier) but to effect took place.

EDIT2: I looked at the error and it says i can not remove the body during a collision event, so how could i remove the body outside?