Jump to content

Collision between an individual sprite and a group of sprites.


gravehopper
 Share

Recommended Posts

Hi, I am almost done a small project I am working on but I am at a bit of an impasse. Even though we are working with phaser my team so far did collisions in a more JS way and I seem to be unable to find a way to do it the phaser way.

 

The set up is sort of like this: 

 

A 3x3 grid made of 9 sprites. They are loaded with a spritesheet (I use mightyeditor with the mt.create to load them into correct locations).

The spritesheet has 4 states. Initial (rocks), water, track and clouds separated as frames. Right now I have a plane flying and not colliding with anything except for the world edges. The plan for this stage of the game is for one plane to fly and the user to tap on the tiles so that they cycle between terrains to make a path (air) for the plane to fly through.

 

What works:

Tiles and the swapping through the frames.

Plane motion.

 

What I need help with:

If the plane has a name 'plane1' and is an individual sprite. How would I check for a collision with individual tiles in the group Blocks which contains 9 sprites. Note that the collision should only happen when the individual tile's key != 1;

 

 

Thanks very much.

Link to comment
Share on other sites

It sounds like you don't need Phaser to check collisions for you. If you maintain this model outside of Phaser and just reflect its state in your game then you can check collisions in your model and let Phaser animate the result.

 

i.e., you know what tiles are in each of the nine positions, why not just check that instead of getting Phaser to check it for you?

Link to comment
Share on other sites

Another option would be to use a CollisionGroup(). 

 

You would need to add each sprite in your grid to one group and then add the individual plane to a separate group.

 

Something like this...

tileCollisionGroup = game.physics.p2.createCollisionGroup();planeCollisionGroup = game.physics.p2.createCollisionGroup();plane = game.add.sprite(x,y, 'planeSprite');plane.body.setCollisionGroup(planeCollisionGroup);//This next line tells the phaser engine what to do when the plane collides with a tile "killPlane"plane.body.collides(tileCollisionGroup, killPlane, this);tiles = this.add.group();tiles.enableBody = true;tiles.physicsBodyType = Phaser.Physics.P2JS;//This just generates 9 sprites in a loop since I'm not sure how you create your 3x3 gridfor (var i = 0; i < 9; i++) { var tile = tiles.create(x,y, 'tileImageSprite'); tile.body.setCollisionGroup(tileCollisiongroup); tile.body.collides([tileCollisiongroup, planeCollisionGroup]); tile.body.immovable = true; this.physics.p2.enable(tile,false);}function killPlane(body1,body2){ //here you could check for what type of sprite body2(which is the tile sprite) is and destroy body1(which is the plane sprite)}

I didn't test this or anything but hopefully it helps you get moving in the right direction. 

 

Good luck! :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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