Jump to content

Find sprites under drop element or mouse pointer

Michel (Starnut)

Recommended Posts

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?




Link to comment
Share on other sites

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.



Link to comment
Share on other sites


  • Recently Browsing   0 members

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