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.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/01/creating-unique-identifiers-for-objects-using-the-getuid-method/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            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);
                Alert.show(text, title);
            }
        ]]>
    </mx:Script>

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

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

</mx:Application>

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?
    Thanks!

    dormouse

  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
    getChildAt(index:int):DisplayObject
    getChildIndex(child:DisplayObject):int
    removeChildAt(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();
    box1.graphics.beginFill(0xFF0000, 0.1);
    box1.graphics.drawRect(0, 0, 100, 100);
    box1.graphics.endFill();
    box1.x = 10;
    box1.y = 10;
    addChild(box1);
    
    var box2:MovieClip = new MovieClip();
    box2.graphics.beginFill(0x00FF00, 0.2);
    box2.graphics.drawRect(0, 0, 100, 100);
    box2.graphics.endFill();
    box2.x = 30;
    box2.y = 40;
    addChildAt(box2, 0);
    
    var label1:TextField = new TextField();
    label1.autoSize = TextFieldAutoSize.LEFT;
    label1.text = String(getChildIndex(box1));
    box1.addChild(label1);
    
    var label2:TextField = new TextField();
    label2.autoSize = TextFieldAutoSize.LEFT;
    label2.text = String(getChildIndex(box2));
    box2.addChild(label2);
    

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

    Hope that helps,
    Peter

Comments are closed.