owen Posted December 8, 2014 Report Share Posted December 8, 2014 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 Quote Link to comment Share on other sites More sharing options...
PMayhem Posted December 9, 2014 Report Share Posted December 9, 2014 I think it should be game.physics.arcade.collide(grpAliens, grpBullets, hitAlien); I think collide accepts 2 more parameters, whether they're required or not I don't know. Will have to look at the docs. Quote Link to comment Share on other sites More sharing options...
owen Posted December 9, 2014 Author Report Share Posted December 9, 2014 Hmmm I tried both collide and overlap but to no avail... Quote Link to comment Share on other sites More sharing options...
PMayhem Posted December 9, 2014 Report Share Posted December 9, 2014 Strange, Have you tried de-bugging and see if simple collisions work first ie: game.physics.arcade.collide(grpAliens, grpBullets); Quote Link to comment Share on other sites More sharing options...
owen Posted December 9, 2014 Author Report Share Posted December 9, 2014 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. Quote Link to comment Share on other sites More sharing options...
owen Posted December 10, 2014 Author Report Share Posted December 10, 2014 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. Quote Link to comment Share on other sites More sharing options...
jpdev Posted December 10, 2014 Report Share Posted December 10, 2014 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. Quote Link to comment Share on other sites More sharing options...
owen Posted December 11, 2014 Author Report Share Posted December 11, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.