Hazel Toh
Hazel Toh

Reputation: 1

AS3 - Hit Test Object between two Movieclips in the same class file

I'm wondering if there is a method to do a hit test object between two movieclips from the same class. The movieclips are being spawned into the stage using a for loop.

Is it possible to if(item(event.currentTarget).hitTestObeject(item(thats not being selected at the moment?))) I'm doing a drag and drop game and am trying to not let the items stack above each other when dragged to the snapping slots on the stage. If not is there other methods avaliable? Thanks in advance (:

Upvotes: 0

Views: 1777

Answers (2)

Ivan Marinov
Ivan Marinov

Reputation: 191

A few weeks ago I was working on a project wich had very specific dragging, so we've decided not to use the Flex DragManager. Our, so called, DragManager is not doing anything special. The thing that I think is related with your work is the presence of snap blocks. In our case the snap blocks were simple groups. Every snap block has a reference to the object wich was dropped into it(let's say it is droppedItem). When trying to drop an item, or even before triggering the snapping mechanism, there is a simple check in the snap block

 if (droppedItem == null)

And this was the solution in our case. I'm not sure what are your snapping slots, but I hope that our approach will help you.

Best regards, Ivan

Upvotes: 0

ghost23
ghost23

Reputation: 2242

first of all, detecting, whether two display objects (sprites, movieclips, etc.) hit each other, is independent of the fact, that the objects are instances of the same class. The important thing is, you have two instances and they are display objects, so you're good to go.

If you want to do hit tests, then i would do the following:

  1. Create a helper class, that holds an Array of all the items, which have already been created.
  2. That helper class needs to add itself as a listener to all the items for the event, that you start drag an item.
  3. Once an item is dragged, the helper class adds itself as a listener to the mouse move event of that item
  4. in the mouse move event handler, the helper class does a hit test from the currently dragged item to each item in its list of items (simple for loop)
  5. if a hit test results with true, you save item you have currently tested against, as this is the one, that you dragged item is overlapping with.
  6. Now you can do measurements (comparing positions, bounding boxes of the two items, etc) to figure out, where to position your dragged item, so that it does not overlap anymore.
  7. Don't forget to remove the mouse move event listener in the event of the user ending to drag.

Upvotes: 1

Related Questions