Nechro Posted November 5, 2014 Share Posted November 5, 2014 I have a group of 'GreenSquares' and I'm wondering how I can check whether any GreenSquare within the group is touching another GreenSquare. I'm also wondering if there's a way I could check if a single GreenSquare is touching not just one, but two other GreenSquares. Any advice at all would be appreciated. Link to comment Share on other sites More sharing options...
lewster32 Posted November 5, 2014 Share Posted November 5, 2014 If you're using Arcade physics, you can do an overlap test on the group:function update() { game.physics.arcade.overlap(GreenSquares, null, function(GreenSquare1, GreenSquare2) { console.log("Touching: ", GreenSquare1, GreenSquare2); });}Unfortunately, each overlap/collision callback is discreet and between two objects, so you'd need to calculate this yourself manually, maybe with a counter (untested code):function update() { // Set the touchCount (a custom property we'll use to count touches) for each square to 0 GreenSquares.setAll("touchCount", 0, false, false, 0, true); game.physics.arcade.overlap(GreenSquares, null, function(GreenSquare1, GreenSquare2) { // Increment the touchCount for the first square involved in this overlap test GreenSquare1.touchCount++; console.log("Touching: ", GreenSquare1, GreenSquare2); }); // Log an array of all squares touching 2 other squares console.log(getSquaresTouching(2));}function getSquaresTouching(count) { // Filter the group children array to return only those with a touchCount of <count> and return it return GreenSquares.children.filter(function(GreenSquare) { return GreenSquare.touchCount === count; })} Link to comment Share on other sites More sharing options...
Nechro Posted November 7, 2014 Author Share Posted November 7, 2014 Thanks Lewster, I was thinking of something like that but had no idea how to implement it in phaser. Unfortunately I've moved from arcade physics to p2 so I'm not sure how much it will apply as there isn't an explicit collide function that I can get the callback from. Link to comment Share on other sites More sharing options...
lewster32 Posted November 7, 2014 Share Posted November 7, 2014 With P2 you use contact events, but the implementation would have to be a little different. I'm afraid I've not used P2 myself so I can't be of much help here. Link to comment Share on other sites More sharing options...
Recommended Posts