Michel (Starnut) Posted December 5, 2013 Share Posted December 5, 2013 Hey everyone, So I've been browsing the examples to find something that fits my needs: I've got a couple of passengers that I want to drag&drop around. When I drop them, I want to check if they've been dropped on a seat and need that seat to perform some detailed checks on it. All seats are in one group. So now I thought I had some options: 1. Collide the passenger with the seat group (not as accurate as I'd like, but a start). I tried it using the Physics collide function, but for some odd reason it never triggered the callback function and kept returning false:this.physics.collide(passenger, this.seats, this.collisionHandler, null, this);(I'm using the project template, hence the this mayhem). Back in Flash I used to have the function dropTarget, which would return all (AFAIR) sprites I dropped the drag object onto. I guess there's nothing like that in Phaser, right? 2. Use the passenger's centered anchor point (or the mouse pointer) to check whether it collides with a seat. Back in Flash I'd use hitTestPoint to perform such a check, but I found nothing like a collidePoint function or anything in the examples or the Physics class. What I found was the following approach using the Phaser.Rectangle in a for loop over the group elements:// backward loop over seats and return first collision with passenger anchor point.for (var i = this.seats.length - 1; i >= 0; i--) { seat = this.seats.getAt(i); if (Phaser.Rectangle.contains(seat.body, passenger.x, passenger.y)) return seat;} I'm very much used to iterating over container children in Flash using the getChildAt function, but I feel very unsure getAt is the way to approach this in Phaser. Is both the recommended way to perform such checks? Or am I missing something? Cheers,Michel Link to comment Share on other sites More sharing options...
rich Posted December 5, 2013 Share Posted December 5, 2013 If you are dragging and dropping sprites then you almost certainly want to use physics.overlap and not collide, as they most likely won't have any velocity so the collision isn't likely to occur. Link to comment Share on other sites More sharing options...
Michel (Starnut) Posted December 5, 2013 Author Share Posted December 5, 2013 Thx, @rich, that sure explains why it didn't work. I missed that velocity has to be set in order for collisions to work and I didn't see it flying over the Physics class. And since you havn't commented on the other code snippet, I figure that's the recommended way to check points against objects. Just wanted to do things properly to fully grasp the concepts behind Phaser. Thanks. Link to comment Share on other sites More sharing options...
rich Posted December 5, 2013 Share Posted December 5, 2013 You're welcome to put the 'get all items under pointer' as a feature request on github, but it's certainly not in there at the moment. Would be such an expensive operation, but I can see useful in some cases. Link to comment Share on other sites More sharing options...
Michel (Starnut) Posted December 5, 2013 Author Share Posted December 5, 2013 I can absolutely see how this would be expensive and by now I'm pretty happy with the rather simple Rectangle.contains approach. I think it's good enough for most cases. Most times you wouldn't want everything returned anyways, I guess. Link to comment Share on other sites More sharing options...
Recommended Posts