Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: referencing movie clips in AS3

  1. #1
    Join Date
    Dec 2007
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile referencing movie clips in AS3

    Hi all. I'm a bit new to AS3 and have a pretty simple question. Here is my code.

    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

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by stevedc View Post
    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.

  3. #3
    Join Date
    Dec 2007
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!
    Last edited by stevedc; 08-25-2008 at 01:44 PM.

  4. #4
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Ok, simple enough...

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

    Code:
    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:

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

    Code:
    this._parent._parent
    You get the idea...

    In AS3, you drop the underscore.

  5. #5
    Join Date
    Dec 2007
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So I'm assuming it would look like this?

    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;
    
    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");

  6. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Correct.

  7. #7
    Join Date
    Dec 2007
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  8. #8
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Code:
    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.

    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.

  9. #9
    Join Date
    Dec 2007
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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"; ?

  10. #10
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    If text_1 and text_2 are still inside mc_background and you're calling this AS from outside of mc_background, then:
    Code:
    mc_background.text_1.autoSize = "center"
    mc_background.text_2.autoSize = "center"
    Again, note the capitalization of autoSize.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •