Welcome, Guest
  • Author Topic: And now let the stupid questions begin...  (Read 2567 times)

    wearHD

    • Server what's that
    • *
    • Posts: 42
      • View Profile
      • Say it Green! - Organic, Fair Trade, Custom Printed Apparel
    And now let the stupid questions begin...
    « on: 08/11/07, 17:35 »
    Ok, so purusing the AS3 board it appears that most of the people who have started using AS3 actually generally know what they're doing.  Mind if a relative newbie joins the conversation?   :)  I am upgrading code from AS2 to AS3 because I need the imrovements made to the FileReference class in AS3.  Upgrading everything is a fair bit of busy-work, but one simple thing is giving me trouble right now.

    The lack of a _level0 or _root reference is tripping me up, since these have been removed in AS3.  At times I want to be able to reference certain arrays by variables instead of simply coding the name of the array.  For example, my AS2 code works with an array of arrays like this:

    Code: [Select]
    var masterArr:Array = new Array("apple", "banana", ... , "fruit");
    var apple:Array = new Array(valueA, ... , valueI);
    var banana:Array = new Array(valA, ... , valN);
    ...
    var fruit:Array = new Array(valueX, ... , valueY);

    ... later ...

    function loadImagesByName() {
       for (var i = 0; i <= masterArr.length; i++) {
           var imgNameToLoad:String = "";
           var name1:String = masterArr[i];
           for (var j = 0; j < _level0[name1].length; j++) { 
              /*Note: _level0[name1] refers to the "apple" through "fruit" arrays declared above*/
              imgNameToLoad = name1 + _level0[name1][j];
              /*go on to load the image, etc.*/
           }
       }
    }

    Without the _level0 reference, how do I access my "apple" through "fruit" arrays by variable names?  Can I just use "[name1][j]" instead of "_level0[name1][j]"?  In AS2 I can't do that... and I can't test right now 'cause there are a ton of other things I need to update in my code before it will compile in AS3.


    Also, a separate question.  Is
    Code: [Select]
    addChild(myChild); exactly the same as
    Code: [Select]
    this.addChild(myChild);?  If not, what are the differences?

    Thanks so much for your help.

    wearHD
    « Last Edit: 08/11/07, 17:42 by wearHD »
    Andy Hultgren
    Say it Green!
    www.sayitgreen.com
    Organic, Fair Trade, Custom Printed Apparel

    wearHD

    • Server what's that
    • *
    • Posts: 42
      • View Profile
      • Say it Green! - Organic, Fair Trade, Custom Printed Apparel
    Re: And now let the stupid questions begin...
    « Reply #1 on: 08/11/07, 22:29 »
    Ok, so the first long complicated question I asked about was indeed a stupid one...  as I continued reading I realized that the "this" keyword doesn't change scope nearly as much in AS3 as is does in AS2, so the line "_level0[name1][j]" can simply be replaced with "this[name1][j]", since my function "loadImagesByName()" is being called from main timeline/stage/level0/root/whatever-it's-called-now-in-AS3.

    Still curious about using "addChild()" vs. "this.addChild()" though... especially from within a function.

    Andy Hultgren
    Say it Green!
    www.sayitgreen.com
    Organic, Fair Trade, Custom Printed Apparel

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re: And now let the stupid questions begin...
    « Reply #2 on: 08/12/07, 02:58 »
    depends on, where you want to add the object to ;)

    there is not realy a difference to AS 2 - just that you have different organisation.
    You have a new basie object for the display arrangement of movieclips - the display object.
    happy flashing
    8)
    Ronald

    wearHD

    • Server what's that
    • *
    • Posts: 42
      • View Profile
      • Say it Green! - Organic, Fair Trade, Custom Printed Apparel
    Re: And now let the stupid questions begin...
    « Reply #3 on: 08/12/07, 15:36 »
    Hi Ronald,

    Thanks for having a look at this question!  Unfortunately I'm a little confused still...  From what I understand, "this.addChild()" and "addChild"  would function identically when called from the "Main Class of the SWF File" (as I guess root/level0/whatever is now called).  Is there any situation where those two lines would function differently?  Embedded in a function (in the main class of the SWF)?  Embedded in method of a class?  Or is "addChild()" simply a shorthand version of "this.addChild()" and both lines would function identically in all cases no matter the context?

    Basically, when "addChild()" is called without being called as a method of any explicitely defined object (eg. "this.addChild()" or "mySprite.addChild()", etc.), it is not clear to me what displayObject the child is actually added to and how that display object is chosen... unless (as I said earlier) "addChild()" is simply a shorthand form of "this.addChild()".  I can't find anything in the documentation addressing this question, though I see both forms used in examples.

    I hope that clarifies my question.  And perhaps you answered it already and I just don't get it, in which case: apologies!
    Andy Hultgren
    Say it Green!
    www.sayitgreen.com
    Organic, Fair Trade, Custom Printed Apparel

    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Re: And now let the stupid questions begin...
    « Reply #4 on: 08/13/07, 01:16 »
    If you call addChild, by default will run in current scope, so it's the same as call this.addChild. If you want to update a specific display list, prepend the proper scope, i.e mySprite.addChild(something)

    Jorge

    wearHD

    • Server what's that
    • *
    • Posts: 42
      • View Profile
      • Say it Green! - Organic, Fair Trade, Custom Printed Apparel
    Re: And now let the stupid questions begin...
    « Reply #5 on: 08/13/07, 23:35 »
    Very cool.  That makes sense.  Thanks Jorge and Richard for wading into my confusion!!   ;D
    Andy Hultgren
    Say it Green!
    www.sayitgreen.com
    Organic, Fair Trade, Custom Printed Apparel