Skip navigation
maxwelladdicted
Currently Being Moderated

Access a code generated MC for hittest

May 8, 2012 9:12 AM

Tags: #cs5 #action_script_3 #gui #snapping

Hi every body

 

I've been looking around without great success to solve this little problem of mine and it goes like this;

 

By clicking two button on stage I create two MovieClips called EVE and BLOCK... That's because the buttons display objects with addChild.

function newMcEvent():void

{

          var eve:MovieClip = new event_graphic();

          eve.x = 50;

          eve.y = 350;

          eveNum++;

          eve.name = "eve " + eveNum;

          addChild(eve);

          eventArray.push(eve);

          for (var i:int = 0; i<eventArray.length; i++)

          {

                    eventArray[i].addEventListener(MouseEvent.MOUSE_UP, touch);

                    eventArray[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);

                    eventArray[i].addEventListener(MouseEvent.MOUSE_UP, noDrag);

                    eventArray[i].addEventListener(MouseEvent.MOUSE_DOWN, snap);

          }

}

 

function newMcBlock():void

{

          var blc:MovieClip = new block();

          blc.x = 50;

          blc.y = 350;

          blcNum++;

          blc.name = "blc " + eveNum;

          addChild(blc);

          blockArray.push(blc);

          for (var i:int = 0; i<blockArray.length; i++)

          {

                    blockArray[i].addEventListener(MouseEvent.MOUSE_UP, touch);

                    blockArray[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);

                    blockArray[i].addEventListener(MouseEvent.MOUSE_UP, noDrag);

          }

}

 

 

BLOCK has the same code as EVE and has also a small invisible area that I want to use for snapping objects (called SNAP, it's a nested movie clip!)...

What I want to do is to add to EVE an EventListener.MOUSE_DOWN that triggers a function called snap... this way I can check if EVE is actually HitTesting the movie clip BLOCK....

I manage to write down this code

 

function snap(e:MouseEvent):void

{

          if (e.currentTarget.hitTestObject(Object(root).blc))

                    {

                              trace("ok");

                    }

}

 

It returns Error #2007: Parameter hitTestObject must be non-null. which I take as the fact it can't find BLOCK or some reason, but it's there I swear!

Any idea?

 
Replies
  • kglad
    61,990 posts
    Jul 21, 2002
    Currently Being Moderated
    May 8, 2012 10:40 AM   in reply to maxwelladdicted

    you made blc local to newMcBlock() so it's not available outside that function.

     

    you can either remove the local function reference or use getChildByName() and use blc's name property to reference it.

     
    |
    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