Batch changing of text color using for loop in AS3

I have a template that has on the stage several movieclips instanced as NameBar1, NameBar2, etc.

I’ve managed to create a background rectangle in each of these movieclips using the following code.

	function makeRectNB():Shape {
		var RectNB:Shape = new Shape();
		RectNB.graphics.beginFill(ColorBg2, 0.9);
		RectNB.graphics.drawRect(64, 0, 456,54);
		RectNB.graphics.endFill();
		return RectNB;
	}
	
	for (var i:uint = 1; i <= 18; i++) {
		this["NameBar" + i].addChildAt(makeRectNB(),0);	
	}

However, I can’t do a much simpler thing, changing the text color of Bib1, Bib2, etc. text fields inside NameBar1, NameBar2, etc using the loop.

NameBar1.Bib1.textColor = ColorTx1;
NameBar2.Bib2.textColor = ColorTx1;

etc. works perfectly, of course. I’m struggling with the reference syntax from within the loop.

Any ideas? Thanks in advance.

What a funny way of doing it. I did not even know, that
this["NameBar" + i].addChildAt(makeRectNB(),0); is possible.

I usually do it basically different. As I expect, from your code, these MovieClips are more or less the same (at least the have the same size). I create one of these MovieClips in the library and add it a class code file. By right click the MovieClip and choosing “Properties”, then activate “Export for ActionScript” and key in a name in the Class field. Then a click to the pencil icon opens a new AS file. There you can add code that is doing stuff like changing colors etc. By declaring these functions public you can later access them from the main AS code of the template. Then drag as many of these MovieClips to the stage as you need. In the code you do a loop over every object on the stage (Google for how to) and if the object type matches your MovieClip class you call these public functions to set whatever you have to set (colors, text, images etc.).

In a similar way you could also do, for instance team lists, with a variable amount of lines, by dynamically add these classes to the stage etc.

Thanks Didi, from a pure conceptual standpoint I’ve understood what you mean, but I need to study AS3 much more… if only I had the time, I work on graphics templates in the evening and that’s it…

For sure your method is much more orthodox than mine, my solution is just what I’ve found googling and googling.

Anyway, in the end looks like that these lines do what I need as well.

	for (var i:uint = 1; i <= 18; i++) {
  		this["NameBar" + i].addChildAt(makeRectBB(),0);
		this["NameBar" + i].addChildAt(makeRectNB(),0);
		this["NameBar" + i]["Bib" + i].textColor = ColorTx1;
		this["NameBar" + i]["N_Surname" + i].textColor = ColorTx2;		
	}		

Thanks for the feedback, and Happy Easter.

Cool! It is always good when something works, that’s what counts, may it be orthodox or not. But when we talk about learning these things: I would suggest, if you ever take time to learn, that you start learning HTML 5 based templates, as Flash is fading away. I myself started to do exactly this a few month back. I can not say, that I am an expert in these thing after this short while, but what I see is, that this approach is much more power full than the old flash way of doing things. So it’s definitely worth it.

1 Like
Privacy Policy   Terms of Service