Actionscript 3, addChild from library then access it's children by instance names
Solution 1
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 :)
Solution 2
You can try like this code:
//prizma, küp, dortgen, koni, kure, silindir are mc's name
var movieList:Array = [prizma, küp, dortgen, koni, kure, silindir];
var k:Number=Math.round(Math.random()*5);
var nes:MovieClip=new movieList [k] ();
addChild(nes);
Solution 3
You need to have a reference to the movieclip that was dynamically added. Then you can access a particular child movieclip if it has an instance name.
So if you have MovieClip with instance name 'a' and inside you have a MovieClip with instance name 'b' then you can simply reference it as a.b. But as Amarghosh pointed out flash will let you have multiple movieclips with the same instance names and if thats the case only one of the movieclips will be accessed.
Solution 4
In AS3, dot notation doesn't work the way it used to in AS2. You can use it if and only if you have explicitly declared the child's name as a variable of parent object. getChildByName
is also not reliable as there is no rule that says two siblings can't have same name. Use getChildAt
to loop through all the children. Try something like:
//assuming newMC as the added movie clip
var num:Number = newMC.numChildren;
for(var i:Number = 0; i < num; i++)
{
var child:DisplayObject = newMC.getChildAt(i);
trace(child.name);
}
Justin Lee
Updated on July 06, 2022Comments
-
Justin Lee almost 2 years
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();
-
M. Ryan over 14 yearsUhh what? Of course dot notation works in AS3, you just have to utilize instance names. If you know what you're trying to access then you shouldn't be looping through children.
-
Amarghosh over 14 yearsDot notation in AS2 works in a totally different way. A parent can access its child by its name like: parentMC.childMC._x = 30; childMC need not be an instance variable of parentMC class for this line to work in AS2. childMC is just the name of the parent.
-
M. Ryan over 14 yearsIt doesn't have to be that way in AS3 either. MovieClips are still dynamic classes in AS3 and you can access them based off of stage names. Your point about duplicate state names is valid, but at worst you'll get a compiler warning. myMC.someStageInstance is still completely valid in AS3. I use it every day.
-
Amarghosh over 14 yearsmyMC.someStageInstance to work in AS3, you should have called myMC.someStageInstance = something; sometime earlier. In AS2 if you call myMC.attachMovieClip with someStageInstance, you can call myMC.someStageInstance to get it.
-
Amarghosh over 14 yearsTo OP: competencyContainer.getChildByName("competency" + i).x can be replaced with competencyInstance.x and that's how you do it in AS3. Treat child movies as separate movieclips rather than referring them thru parents. Store them in an array and loop thru them if you want.
-
Amarghosh over 14 yearsgetChildByName returns a DisplayObject. You have to cast it to competencyCircle to access the nameText property. It is: competencyCircle( competencyContainer.getChildByName(childName)).nameText;
-
Amarghosh over 14 yearscompetencyContainer.getChildByName("competency" + i).getChildByName("nameText").wordWrap = true; gives the 1061 error because getChildByName returns a DisplayObject and getChildByName is a method of DisplayObjectContainer class. You have to cast.
-
Amarghosh over 14 yearsIf nameText is a property of competencyCircle class, you can access it using var cc:competencyCircle = competencyCircle( competencyContainer.getChildByName("competency" + i)); and then call cc.nameText;
-
M. Ryan over 14 yearsOh, you're talking about child clips that were dynamically added. You misread his question. He's already dynamically added a clip from the library, now he is trying to access children of the clip he added, presumably these children were already on the stage in the library. Maybe the OP should better word his question, it is a little confusing.