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 ?
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.
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.
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).
Ah, you can use the function getObjectsUnderPoint which is under the DisplayObjectContainer class, so Sprites should have that function. You should look at this.
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?
Returns an array of objects that lie under the specified point and are
children(or grandchildren, and so on) of this DisplayObjectContainer
instance.
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.