Skip navigation
maxwelladdicted
Currently Being Moderated

HitTestObject with a movieCLip child

Jun 6, 2012 3:15 AM

Tags: #problem #actionscript3 #child #parent #hittestobject #snap

Hey all

I've been banging my head around this since yesterday and I can't really understand what's the problem...

 

1. I have created on stage three movieclip from the library called Eve, Sta, and Act... If Eve or Act touch Sta they each snap ina specific position relatively to Sta...

Screen shot 2012-06-06 at 11.54.18 AM.png

 

2. Let's say that Eve touches Sta and it snaps in place... They will now move as one. This is made possible because I placed them inside an empty MovieCLip on stage called Container... Up to here the code works fine; they snap and move around like they should

Screen shot 2012-06-06 at 12.03.47 PM.png

 

3. I want now to snap Act to Sta too, meaning (I guess) that if Act touches the child of Container, called Sta or the child in position 0 (Sta is always in position 0 inside Container), it should snap into position relatively to Container's child Sta....

 

the snapping code works like this

function snap(e:MouseEvent):void

{

          var container:MovieClip = new MovieClip();

          container.name = "ao";

 

          if (e.target.name == "eve" && DisplayObjectContainer(e.target).hitTestObject(this.getChildByName("s ta")))

          {

                    DisplayObjectContainer(e.target).x = DisplayObjectContainer(this.getChildByName("sta")).x - 78;

                    DisplayObjectContainer(e.target).y = DisplayObjectContainer(this.getChildByName("sta")).y;

                    container.addChild(DisplayObjectContainer(this.getChildByName("sta")) );

                    container.addChild(DisplayObjectContainer(this.getChildByName("eve")) );

          }

 

 

          if (e.target.name == "act" && DisplayObjectContainer(e.target).hitTestObject(this.getChildByName("s ta")))

          {

                    DisplayObjectContainer(e.target).x = DisplayObjectContainer(this.getChildByName("sta")).x;

                    DisplayObjectContainer(e.target).y = DisplayObjectContainer(this.getChildByName("sta")).y + 75;

                    container.addChild(DisplayObjectContainer(this.getChildByName("sta")) );

                    container.addChild(DisplayObjectContainer(this.getChildByName("act")) );

          }

          else if (e.target.hitTestObject(container.getChildByName("sta")))

          {

                    trace("ok");

          }

 

 

          container.addEventListener(MouseEvent.MOUSE_DOWN, drag);

          container.addEventListener(MouseEvent.MOUSE_UP, noDrag);

          container.buttonMode = true;

//feedback

          trace(container.x);          // returns 0

          trace(container.y);         //  returns 0

          trace(container.getChildAt(0)); //returns the name of the obj from the library

          trace(container.getChildByName("sta")); //returns the name of the obj from the library

          addChild(container);

}

 

 

To finish, the problem I'm having is that if I snap something to Sta, any other attempt to attach another mc will fail, so the snapping works fine for the forst time, but not for any other attempt...

 
Replies
  • Currently Being Moderated
    Jun 6, 2012 4:47 AM   in reply to maxwelladdicted

    Just on a cursory review of the code, the problem (with the error) likely lies in testing...

     

         this.getChildByName("sta")

     

    As soon as you place the sta object inside the container it is no longer a direct child of 'this', but is instead a child of 'container'

     

    There are likely a few ways to deal with this. One would be to always have sta inside the container right from the start, and then hit test against  container.getChildByName("sta").  Another way could be to not use the getChildByName approach and instead store references to the actual objects, and then just hit test the objects.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points