Jump to content

collision detection simply not working


owen
 Share

Recommended Posts

This is almost certainly another newbie fail from me, but I am having trouble getting groups of sprites to collide with each other in my simple 'shoot the aliens' game.

 

In my update() function I have this:

// check collisions        game.physics.arcade.overlap(grpAliens, grpBullets, hitAlien);      
  
and then I have a function hitAlien() like this:
 
function hitAlien(alienSprite, bulletSprite) {    if (alienSprite.y > 0) {        console.log("hitAlien");        bulletSprite.kill();        alienSprite.kill();        alienSprite.destroy();    }}
 
The problem is, the bullets (in grpBullets) simply glide over the top of the aliens (grpAliens) without colliding with them.  I don't see any reason why this could happen.
 
What might I be doing wrong?
 
The work-in-progress code is here:  http://www.owensouthwood.com/spacejunk
 
Any help would be much appreciated.
 
Thanks,
Owen
 
 
Link to comment
Share on other sites

I'll try.   One thing is that I'm using sprites attached to each other for my aliens.  The aliens comprise of 3 sprites (head, torso, feet) attached to each other in a holding 'alien' sprite which then lives in the grpAliens group. So this may be the root cause of my problem.  I am going to try what you said, also I will try putting the head, feet and torso individually into the grpAliens group.

Link to comment
Share on other sites

Thanks for the replies.  I've got around the problem successfully although not exactly solved it.  Basically I was trying to randomly generate 3-part aliens with 3 sprites as children of a parent sprite.  The collision did not work with that no matter what I did.  So I gave up and used single sprites instead, and the collision works fine now.  It seems that using the addChild function causes the collision detection to fail, but probably there is a fix for that which I don't know about.

Link to comment
Share on other sites

Hi owen,

 

addChild does not break collision.

 

(For example http://jppresents.net/static/tankwars the turret is a child of the tank and the tank tracks too - and arcade collision and overlap both work fine )

 

The main sprite should be the biggest part of your alien (and it defines the collision size).

You have to enable the physics on only this sprite and do collision checks against this main sprite. (you can increase the body dimenions after enabling phsyics, so the body is bigger then just this sprite).

 

I hope this helps.

 

 

 

 

Link to comment
Share on other sites

Hi owen,

 

addChild does not break collision.

 

(For example http://jppresents.net/static/tankwars the turret is a child of the tank and the tank tracks too - and arcade collision and overlap both work fine )

 

The main sprite should be the biggest part of your alien (and it defines the collision size).

You have to enable the physics on only this sprite and do collision checks against this main sprite. (you can increase the body dimenions after enabling phsyics, so the body is bigger then just this sprite).

 

I hope this helps.

 

Thanks.  My alien was comprised of 3 equally sized parts (head, torso, feet) stacked on top of each other.  So there was no 'biggest part'.  The idea was that I randomly generate an alien from a selection of heads, torsos and feet sprites.  I think from what you said, the solution would be to carry on doing that but increase the body size of the head to encompass all 3 parts, therefore the collision would be handled.  Anyhow I have abandoned the random alien idea and created single sprites instead, for simplicity.  What I might do however is create 'boss' aliens that have gun turrets etc. that can be independently destroyed, and I'll use the technique you described for that.

 

Owen

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...