Actionscript 3, addChild from library then access it's children by instance names

Justin Lee picture Justin Lee · Sep 25, 2009 · Viewed 27.5k times · Source

So I successfully added a movie clip from the library using addChild(), but now I want to access some movieclips that were in that dynamically added movieclip.

I've used standard dot notation and also getChildByName passing it the instance names.

What am I missing here?

---- EDITED ----

I tried the suggestion of looping through and can access them by index, but seems like not as intuitive of a way to do this... below are the two examples of what I had previously tried and they all come back as "1119: Access of possibly undefined property nameText through a reference with static type flash.display:DisplayObject." or "1061: Call to a possibly undefined method getChildByName through a reference with static type flash.display:DisplayObject."

example of dot notation:

 //  build circlular display
function buildCircle() {
    trace("buildCircle()");
    if (viewByState == "assignment") {

        var competencyContainer:MovieClip = new MovieClip();
        competencyContainer.name = "competencyContainer";
        this.addChild(competencyContainer);

        var angleSegment:Number = 360 / competenciesArray.length;
        var angleSum:Number = 360 - angleSegment / 2;

        for (var i:Number = 0; i < competenciesArray.length; i++) {

            var competencyInstance:competencyCircle = new competencyCircle();
            competencyInstance.name = "competency" + i;
            competencyContainer.addChild(competencyInstance);


            competencyContainer.getChildByName("competency" + i).x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
            competencyContainer.getChildByName("competency" + i).y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));


            competencyContainer.getChildByName("competency" + i).nameText.wordWrap = true;
            competencyContainer.getChildByName("competency" + i).nameText.embedFonts = true;
            competencyContainer.getChildByName("competency" + i).nameText.htmlText = "COMPETENCY:<br />" + competenciesArray[i].name;

            competencyContainer.getChildByName("competency" + i).nameText.setTextFormat(tfTitle, 0, competencyContainer.getChildByName("competency" + i).nameText.text.length - competenciesArray[i].name.length);

            competencyContainer.getChildByName("competency" + i).nameText.setTextFormat(tfName, competencyContainer.getChildByName("competency" + i).nameText.text.length - competenciesArray[i].name.length, competencyContainer.getChildByName("competency" + i).nameText.text.length);

            competencyContainer.getChildByName("competency" + i).nameText.autoSize = TextFieldAutoSize.CENTER;

            competencyContainer.getChildByName("competency" + i).nameText.y = -(competencyContainer.getChildByName("competency" + i).nameText.height / 2);

            competencyContainer.getChildByName("competency" + i).filters = [circleDefaultDropShadow];
            competencyContainer.getChildByName("competency" + i).selectedIndicator.visible = false;

            competencyContainer.getChildByName("competency" + i).hit.buttonMode = true;
            competencyContainer.getChildByName("competency" + i).hit.mouseEnabled = true;
            competencyContainer.getChildByName("competency" + i).hit.tabEnabled = true;
            competencyContainer.getChildByName("competency" + i).hit.mouseChildren = true;

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleHoverDropShadow];
                                                                                     });

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleDefaultDropShadow];
                                                                                     });

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleDefaultDropShadow];
                                                                                     e.target.parent.selectedIndicator.visible = true;
                                                                                     });

            angleSum -= angleSegment;
            trace("end");
            trace(i);
            trace("\n\n\n");
        }
    } else if (viewByState == "competency") {



    } else {

    }
}
buildCircle();

and example using .getChildByName():

    //  build circlular display
  function buildCircle() {
    trace("buildCircle()");
    if (viewByState == "assignment") {

        var competencyContainer:MovieClip = new MovieClip();
        competencyContainer.name = "competencyContainer";
        this.addChild(competencyContainer);

        var angleSegment:Number = 360 / competenciesArray.length;
        var angleSum:Number = 360 - angleSegment / 2;

        for (var i:Number = 0; i < competenciesArray.length; i++) {

                var competencyInstance:competencyCircle = new competencyCircle();
                competencyInstance.name = "competency" + i;
                competencyContainer.addChild(competencyInstance);


                competencyContainer.getChildByName("competency" + i).x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
                competencyContainer.getChildByName("competency" + i).y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));


                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").wordWrap = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").embedFonts = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").htmlText = "COMPETENCY:<br />" + competenciesArray[i].name;

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").setTextFormat(tfTitle, 0, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length - competenciesArray[i].name.length);

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").setTextFormat(tfName, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length - competenciesArray[i].name.length, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length);

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").autoSize = TextFieldAutoSize.CENTER;

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").y = -(competencyContainer.getChildByName("competency" + i).getChildByName("nameText").height / 2);

                competencyContainer.getChildByName("competency" + i).filters = [circleDefaultDropShadow];
                competencyContainer.getChildByName("competency" + i).getChildByName("selectedIndicator").visible = false;

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").buttonMode = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").mouseEnabled = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").tabEnabled = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").mouseChildren = true;

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleHoverDropShadow];
                                                                                                                                                                 });

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleDefaultDropShadow];
                                                                                                                                                                 });

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleDefaultDropShadow];
                                                                                                                                                                 e.target.parent.getChildByName("selectedIndicator").visible = true;
                                                                                                                                                                 });

                angleSum -= angleSegment;
                trace("end");
                trace(i);
                trace("\n\n\n");
        }
    } else if (viewByState == "competency") {



    } else {

    }
}
buildCircle();

Answer

Tomas picture Tomas · Aug 13, 2010

What about using this when create the clips:

...
competencyInstance.name = "competency" + i;  
competencyContainer.addChild (competencyInstance);          
// this is the additional line, cecessary for accesing later by name
competencyContainer["clipFichaEjemplo2"] = competencyInstance;
...

And this to access them later:

...
var M:MovieClip = clipMapa["competency" + i];
...

it worked for me in a similar flash project where i have the problem using addChild dynamically.

ihih: I hope it helps :)