[SOLVED] Mouse event when dragging sprite

I’m new so hello :smile:

I have game with board and some cards. I can drag my cards to the board . Board is build from other sprites. and I want to detect on what board part im dragging my card.

I have this.addEventListener (MouseEvent.MOUSE_OVER, setActive); on every board parts and it’s working when I’m moving mouse on top of each element but not when I’m dragging card over board.

how can I propagate this event to the board when dragging other sprite.
I know problem is sprite that I;m dragging is always on the same position that mouse is so it covers board all the time, but can I somehow propagate this event ?

What shape are your boards? If it’s relatively simple it may be easier to write a small function to check if the mouse is inside the shape.

yup but if I have board made from for example 64 sprites than i have to check every one of them. also my board pieces are not square :slight_smile:

I would have made only one polygon per board not per sprite,

it seems that the MOUSE_OVER event is only dispatched to the top-most sprite,
there’s ROLL_OVER which takes into account children, but since your card isn’t a child of each board it wouldn’t work.

Not necessarily, it’s still possible.

What you could do is take a snapshot of the game board with the positions and sizes of each shape and store it into an array (putting individual sizes and shapes into a Rectangle), then on your “drop” event you can detect which “rectangle” the mouse is in and then do whatever you need to do on the drop event.

private var _snapshot:Array<Rectangle> = [];

private function onCardDrop(e:MouseEvent):Void
    for (i in 0..._snapshot.length)
        var r = _snapshot[i];
        if (e.localX > r.x && e.localX <= r.x + r.width && e.localY > r.y && e.localY <= r.y + r.height)
            //Do something with card

This example works best with squares, or square-like shapes. Alternatively, you could use Box2D and its Region tool to define the regions of each shape on your gameboard, and iterate over the snapshot of those regions, but that may cause a drop in performance. Up to you, really.

hi, thx for your replay,
or it is posible to get sprite/all sprites on given x/y position?

You mean on the gameboard? Since the snapshot would merely be a reference to the gameboard with just the positions and sizes, you can implement a function that simply gets the gameboard tile with the given snapshot reference for when you drop the card.

If that’s what you mean.

I also meant stageX and stageY in the event variable in my previous example, as that takes the mouse position relative of the stage, not the target (which would in this case be the card itself).

no, what i ment is do openfl have a function / something that can get x,y position and return all sprites on this position.

like Board.hitTestPoint (X, Y) do for one sprite but for all of them and return all sprites on the stage that are on this position

Ah, you can use the function getObjectsUnderPoint which is under the DisplayObjectContainer class, so Sprites should have that function. You should look at this.

I did that. on my card:

private function onMouseMove (event:MouseEvent):Void {
		if (this.Player.Board.hitTestPoint (event.stageX, event.stageY)  ) {
			trace(this.Player.Board.getObjectsUnderPoint(new Point(event.stageX, event.stageY)));

but all I get is:

Card.hx:173: []
Card.hx:173: []
Card.hx:173: []
Card.hx:173: []
Card.hx:173: []

and board is sprite that have children sprites nothing else.

What happens if you call the getObjectsUnderPoint on the stage instead of your Board?

than i got Card.hx:173: [[object Bitmap],[object Bitmap],[object Bitmap],[object Bitmap],[object Bitmap],[object Bitmap]]
but according to documentation it should be working, am i wrong?

even if i make

instead of

it is working ( return card and board field)
i can work with that, but my point is…it should be working on board object :slight_smile:

to show you my point i made this:

for(x in this.Player.getObjectsUnderPoint(new Point(event.stageX, event.stageY))){
			trace(x.parent, '---->', x.parent.parent);

and result is:

Card.hx:174: [object Card],---->,[object Hand]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Card],---->,[object Hand]
Card.hx:174: [object Card],---->,[object Hand]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Hex],---->,[object Board]
Card.hx:174: [object Card],---->,[object Hand]
Card.hx:174: [object Card],---->,[object Hand]

Returns an array of objects that lie under the specified point and are
children(or grandchildren, and so on) of this DisplayObjectContainer

that means its only going to return the children of the Board, and if there are no children, expect no results.

Judging by how your code works, you should make the Board class contain the Sprite children of your cards, players and other objects, instead of attaching the Board to the Player which, in my opinion, makes no sense.

It makes sense to have multiple players and AI on the same game board, but to have the Board specific to each player is odd game logic to say the least.

it’s not attached it’s just reference to the board.
and Board do have children’s! 52 of them and they are all Hex class instance.

parenting going like that in my case:

stage -> player
stage -> board

player -> hand -> cards x 7
board -> hex x 52


thx guys for help :smile:

dont know why but code:

for(x in this.Player.Board.getObjectsUnderPoint( this.Player.Board.globalToLocal(new Point(event.stageX, event.stageY) ) ))

just works as expected. thx