Log in

View Full Version : referencing movie clips in AS3



stevedc
08-25-2008, 05:49 AM
Hi all. I'm a bit new to AS3 and have a pretty simple question. Here is my code.


function loadXML(loaded) {
if (loaded) {

_root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

text_1.text = _root.inventor;
text_2.text = _root.comments;

} else {
trace("file not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("inventors.xml");

but what if the text boxes (text_1 and text_2) are inside of a movieclip called mc_background? how do I reference this in the code - obviously not using _root...but?

I'm a newbie as you can see, but am working on it :)

~Regards
Steven

Medyman
08-25-2008, 12:12 PM
Hi all. I'm a bit new to AS3 and have a pretty simple question. Here is my code.

but what if the text boxes (text_1 and text_2) are inside of a movieclip called mc_background? how do I reference this in the code - obviously not using _root...but?

I'm a newbie as you can see, but am working on it :)

Is that the code you're using? Or is that the code you'd like to convert to AS3? Because...that code is in AS2.

I can show you how to do it in either. Just need to know which one you intended to use.

stevedc
08-25-2008, 12:15 PM
Actually, I think it might be best if I do this in AS2. What would be the best way to do this?..and thanks in advance!

Medyman
08-25-2008, 01:44 PM
Ok, simple enough...

If text_1 is inside mc_background, you would reference it as such:


mc_background.text_1.text = blahBlahBlah;

So you know...It's considered problematic to be using _root. Once you get into things like embedding things within each other, it's very hard to confuse which timeline _root refers to. It's much better to use the this._parent syntax (in AS2).

For example, if you're referring to the main timeline from within mc_background (a movieclip on the main timeline), you should say:


this._parent

If you're referring to the root timeline from within a movieclip within mc_background, you would say:


this._parent._parent

You get the idea...

In AS3, you drop the underscore.

stevedc
08-25-2008, 02:08 PM
So I'm assuming it would look like this?


function loadXML(loaded) {
if (loaded) {

_root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

mc_background.text_1.text = _root.inventor;
mc_background.text_2.text = _root.comments;

} else {
trace("file not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("inventors.xml");

Medyman
08-25-2008, 02:25 PM
Correct.

stevedc
08-25-2008, 10:01 PM
amazing. Medyman - many thanks - works great. Unfortunately I have another question.

I'm pulling in XML text / content into text fields. this much we already know. However, I would like these text fields to have a background color of white, a 1px border, be %30 transparent and expand if the XML content grows or updates in the future.

something like autosize: true?

Medyman
08-25-2008, 10:26 PM
something like autosize: true?

Close. The property is autoSize (note the capitalization), but it's not a boolean.

The options are:

"none" : does not resize
"left" : expands to the right & bottom.
"right" : expands to left & bottom
"center" : expands to both sides and bottom

More info (http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary729.html).

The rest of the formatting changes can be made directly to the textfield. Or do you want to do them programatically? That's possible too but not necessary if the text fields are manually created.

stevedc
08-26-2008, 04:08 AM
this resource is def helpful. But if my code hasn't change, where exactly would I put this. (baby steps - I know : (..)

textField2.autosize = "center";

so text_1.autosize = "center"; ?

Medyman
08-26-2008, 10:51 AM
If text_1 and text_2 are still inside mc_background and you're calling this AS from outside of mc_background, then:


mc_background.text_1.autoSize = "center"
mc_background.text_2.autoSize = "center"

Again, note the capitalization of autoSize.

stevedc
08-26-2008, 07:45 PM
medyman - worked perfectly and thank you!! I'm working a new contract and even though they hired me for design and front end, everything has been actionscript so far - and I'm barely getting through this....:(

Medyman
08-26-2008, 10:38 PM
medyman - worked perfectly and thank you!! I'm working a new contract and even though they hired me for design and front end, everything has been actionscript so far - and I'm barely getting through this....:(

Honestly, if this is a paid contract, you might (should!) consider subcontracting the job. At least, the relevant Flash parts.

stevedc
08-26-2008, 11:01 PM
Of course!..but unfortunately right out of school I'm not getting paid enough per hour to make it worth it :(. Plus I need to spend the next year really understand this language and how other OOP langs work. Using the timeline was easy, but learning how to control all of this with code - not so much.

Medyman
08-26-2008, 11:09 PM
I understand, but I'm not sure learning during the client work is the best way of going about it. Of course, in the web industry learning never stops. New languages, new techniques, new "best practices"are being developed all the time. So, my advice is to take work within your scope of knowledge and work to enhance or increase that scope away from a paid shop.

Please don't be offended. This is just a trend in the web industry I find very disturbing. Whether you realize it or not,you're hurting yourself and the rest of the web industry more than your gaming and that paycheck from doing this kind of work.

okay, I'm done.