3 Replies Latest reply on Jun 4, 2009 2:44 PM by BennyBeta

    array.push outside of function?

    saveth Level 1

      I'm using the new Flash Builder beta to put together a quick proof of concept. Do you see any problems with the code below?

       

      <fx:Script>

           <![CDATA[

                private var contentArray:Array = new Array();

                contentArray.push("content/1.mov");

                contentArray.push("content/2.mov");

                contentArray.push("content/3.mov");

           ]]>

      </fx:Script>

       

      This comes back with 1120: Access of undefined property contentArray on all 3 lines where I'm using .push.

       

      it works if I populate the array from inside a function but I don't really understand why or what the difference would be. Any help is you can give would be appreciated.

       

      Thanks,

      Brian

        • 1. Re: array.push outside of function?
          BennyBeta Level 4

          You're only really supposed to declare (and optionally initialize) variables outside of functions.

           

          The code outside of functions is used for defining the class.  It is not executed "top to bottom" like code inside functions.

           

          Sorry, I wish I had a better explination than "your just not allowed to do that"...

          • 2. Re: array.push outside of function?
            saveth Level 1

            Sometimes that's the only answer there is. It just seems strange they will let you do this:

             

            <fx:Script>

                 <![CDATA[

                      private var contentArray:Array = new Array("content/1.mov", "content/2.mov", "content/3.mov");

                 ]]>

            </fx:Script>

             

            but not this:

             

            <fx:Script>

                 <![CDATA[

                      private var contentArray:Array = new Array();

                      contentArray.push("content/1.mov");

                      contentArray.push("content/2.mov");

                      contentArray.push("content/3.mov");

                 ]]>

            </fx:Script>

             

            Thanks for you help.

             

            Brian

            • 3. Re: array.push outside of function?
              BennyBeta Level 4

              Your welcome.  I'm glad you figured out how to do it...