Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

actionscript 3 - as3 accessing Children from dynamicly added moiveclip

I am adding moiveClips and putting their position accordingly. I am placing all the dynamically added clips in one new movie clip and then centering that on stage. As a test, I see I can control the alpha in the main container but I can not access the children. How can I assess each flagButton separately? There would be several in this loop example below.

var flagButton:MovieClip;
var flagArray:Array = new Array;
var flagDisplayButtons:MovieClip = new MovieClip();
function displayFlagButtons()
{




for( var i = 0; flagArray.length > i; i++)
{
    var flagButton:MovieClip = new roundButton();
    flagButton.x = (flagButton.width + 10) * i;
    flagButton.y = stage.stageHeight - flagButton.height;
    addChild(flagButton);

    flagDisplayButtons.addChild(flagButton);
}
addChild(flagDisplayButtons);

// Works
flagDisplayButtons.x = stage.stageWidth/2 - flagDisplayButtons.width/2;

// Does not see flagButton
flagDisplayButtons.flagButton.alpha = .5;
}

GETS ERROR: TypeError: Error #1010: A term is undefined and has no properties.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your example code, you have created a bunch of button instances, and one by one you have added them to the display list of flagDisplayButtons. They are on that display list in the order that you placed them.

first instance is at index 0

second instance is at index 1

third instance is at index 2

and so on.

So if you want to access the third button you added, you could do this :

var flagButton:RoundButton = flagDisplayButtons.getChildAt(2) as RoundButton;

Now that you have created variable to reference that button, you can do any of these :

flagButton.alpha = .5;
flagButton.x = 100;
flagButton.y = 200;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...