0
votes

I still have a little problem. I have 6 diferent objects. Let's call them a, b, c, d, e, f. Each of these objects have containd objects (buttons), and I use them as some kind of hit zone (see picture in a link)!

http://postimg.org/image/j2h4y2c65/

Both of objects in picture are movable objects. Each of objects have start positions. I want to do this:

If object a.hitzoneA collides with b.hitzoneB (a.hitzoneA.hitTestObject (b.hitzoneB)), return object "a" to the start position, and leave object "b" where it is. Vice Versa, if object b.hitzoneB hits a.hitzoneA, I want object b to move to its start position.

I tryed using if/else if/else, arrays of objects, but no luck. BOTH objects go to its start positions, every time. Can someone suggest solution to this problem?

http://pastie.org/private/gfrxgzsv4jj7qdvh3vzphq

1

1 Answers

0
votes

Any time Object A's hitbox overlaps with Object B's hitbox, the inverse is also true. So doing a hitTest will always be true for both objects at the same time. You can cancel the second hitTest when the first one returns true, but that will just result in object A always moving back to its start position, and never object B.

It seems like you only want the object that is moving to go back to its start position, like if the user is using the mouse to click and drag that object to another position. If so, check for the hitTest as soon as the user tries to place it somewhere. Then move only that object back to its start position if the hitTest returns true.

EDIT:

Here's an example onMouseReleased function (passed to your event listener):

private function onMouseReleased(e:Event):void{
     var movingObject:MovableObject = getMovingObject(); //returns reference to the object being moved.  Can save this reference when the user first clicks on the object to move.
     if(movingObject == a && a.hitzoneA.hitTestObject(b.hitzoneB)){
         a.x = startAx;
         a.y = startAy;
     }
     else if(movingObject == b && b.hitzoneB.hitTestObject(a.hitzoneA)){
         b.x = startBx;
         b.y = startBy;
     }
     movingObject.stopDrag();
}

This is still a rather clunky way to do what you want. It would scale far better using a more object-oriented approach. However, something like this could work.