Creating unique identifiers for objects using the getUID() method

The following example shows how you can use the static UIDUtil.getUID() method to generate and retreive a unique identifier for an Object. The first time you click the Display UID button, an Alert control displays the generated UID and a string representation of the Object (yay ObjectUtil.toString()!). The second time you click the Display UID button, you’ll notice that a new parameter is added to the Object, mx_internal_uid.

Full code after the jump.


<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application xmlns:mx=""

            import mx.controls.Alert;
            import mx.utils.ObjectUtil;
            import mx.utils.UIDUtil;

            private function displayUID():void {
                var text:String = ObjectUtil.toString(myObj);
                var title:String = UIDUtil.getUID(myObj);
      , title);

    <mx:Object id="myObj" name="peter" site="" />

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Display UID"
                click="displayUID();" />


View source is enabled in the following example.

3 thoughts on “Creating unique identifiers for objects using the getUID() method

  1. peterd,
    i am sorry to bother you again, i have a problem about convert MovieClip from as2.0 to as3.0. in as2.0, MovieClip has a method calling createEmptyMovieClip(String name, int depth), but in as3.0, the MovieClip doesn’t have this method. How to convert, especeilly , how to solution the depth?


  2. dormouse,

    And an excellent question that is. The MovieClip class changed somewhat significantly between ActionScript 2.0 and ActionScript 3.0. Now, in ActionScript 3.0 you use the “new MovieClip()” constructor to create a new movie clip (which is a lot more consistent with the rest of the Flash Player API, ie: new Sprite(), new MovieClip(), new Array(), new Date(), new Video(), etc).

    As for depth, there are several methods for handling depth. In no particular order:

    addChildAt(child:DisplayObject, index:int):DisplayObject
    setChildIndex(child:DisplayObject, index:int):void
    swapChildren(child1:DisplayObject, child2:DisplayObject):void
    swapChildrenAt(index1:int, index2:int):void

    (Taken from the MovieClip class in the Flash CS3 LiveDocs.)

    So, the general idea is that the “DisplayObject” in question is your MovieClip symbol, and the “index” is the current depth (or desired depth, depending on the method).

    Here’s a pretty crude example of a few of the methods (I tested this in the main timeline of Flash CS3, since I already had it open, you may need to tweak somewhat if you’re using Flex):

    var box1:MovieClip = new MovieClip();, 0.1);, 0, 100, 100);;
    box1.x = 10;
    box1.y = 10;
    var box2:MovieClip = new MovieClip();, 0.2);, 0, 100, 100);;
    box2.x = 30;
    box2.y = 40;
    addChildAt(box2, 0);
    var label1:TextField = new TextField();
    label1.autoSize = TextFieldAutoSize.LEFT;
    label1.text = String(getChildIndex(box1));
    var label2:TextField = new TextField();
    label2.autoSize = TextFieldAutoSize.LEFT;
    label2.text = String(getChildIndex(box2));

    For more information, you can read the “Display programming” chapter of Programming ActionScript 3.0.

    Hope that helps,

Comments are closed.