Skip navigation
NickelBuckle9
Currently Being Moderated

How to obtain instance name of Movie Clip?

Jul 18, 2012 10:54 AM

Tags: #as3 #array #variable #movie #clip #mc

Hello!

 

Is there a way to get the instance name of a move clip once it's on the stage?  In my dress up game, I need to know which items are on the doll in order to keep them visible.  My drag and drop feature uses an array and currentTarget:

 

 

var dragArray:Array = [Doll.Drawers.Dress1, Doll.Drawers.Dress2, Doll.Drawers.Dress3, Doll.Drawers.Dress4];
          
          for(var i:int = 0; i < dragArray.length; i++) 
            {
                    dragArray[i].buttonMode = true;
                    dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
                    dragArray[i].addEventListener(MouseEvent.MOUSE_UP, item_onMouseUp);
            }


function item_onMouseDown(event:MouseEvent):void 
          {
               var clip:MovieClip = MovieClip(event.currentTarget);
               clip.startDrag();
          }
  
function item_onMouseUp(event:MouseEvent):void 
          {
               var clip:MovieClip = MovieClip(event.currentTarget);
               clip.stopDrag();
               if(clip.hitTestObject(Doll.Skins))
                    {
                             //Here's where the problem starts!   ----------------------------------------------  //
                              trace("It's on the doll!");
                    }
          }

 

It can successfully run this code.  However, instead of tracing "It's on the doll!", I'd like to turn the currentTarget into it's instance name, which should be "Doll.Drawers.Dress1" etc... and then store that name in an array.

 

How would I do this?

 

I've looked into e.target.name, but I keep getting errors...

 
Replies
  • Currently Being Moderated
    Jul 18, 2012 11:03 AM   in reply to NickelBuckle9

         event.target.name

     

    only if you asign the instance name from the properties panel

     

    if you create the object with code you nedd asigna a name:

     

      var clip:MovieClip = MovieClip(event.currentTarget);

         clip.name="theName"


     
    |
    Mark as:
  • kglad
    72,189 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2012 11:03 AM   in reply to NickelBuckle9

    use the name property of clip (if that's the movieclip whose name you want):

     

     

     

     

    var dragArray:Array = [Doll.Drawers.Dress1, Doll.Drawers.Dress2, Doll.Drawers.Dress3, Doll.Drawers.Dress4];
              
              for(var i:int = 0; i < dragArray.length; i++) 
                {
                        dragArray[i].buttonMode = true;
                        dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
                        dragArray[i].addEventListener(MouseEvent.MOUSE_UP, item_onMouseUp);
                }
    
    
    function item_onMouseDown(event:MouseEvent):void 
              {
                   var clip:MovieClip = MovieClip(event.currentTarget);
                   clip.startDrag();
              }
      
    function item_onMouseUp(event:MouseEvent):void 
              {
                   var clip:MovieClip = MovieClip(event.currentTarget);
                   clip.stopDrag();
                   if(clip.hitTestObject(Doll.Skins))
                        {
                                 //Here's where the problem starts!   ----------------------------------------------  //
                                  trace(clip.name);  // but that won't be Doll.Drawers.Dress1.  it might be Dress1.
                        }
              }
    
    
     
    |
    Mark as:
  • kglad
    72,189 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2012 11:45 AM   in reply to NickelBuckle9

    you're welcome.

     

    p.s.  please mark esdebon's answer as helpful.

     
    |
    Mark as:
  • kglad
    72,189 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2012 12:40 PM   in reply to NickelBuckle9

    thank you!

     
    |
    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