简体   繁体   中英

Story of a mysterious bug Flex/ Actionscript

Suddenly Flex seems to dislike variable declaration. For example I write (on the script part of a mxml component)

    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;


            var i:int = 1;
            while(i< 9) i++;

            [Bindable]
              public var evolution:ArrayCollection = new ArrayCollection();


        ]]>
   </mx:Script>

And it says the variable i has not been defined. This doesn't make any sense to me. Any guess of what might have gone wrong? It happened all of a sudden, when I put the evolution ArrayCollection calling the simple constructor with no arguments. I wanted to add items using a while cycle instead, but now I've erased nearly all the code and I can't figure out what went wrong it doesn't seem to recognize my variables anymore! I'm going crazy.

If you wrap your loop in a function, you'll get past this issue.

As a matter of fact, anytime you try to run code outside of a function you'll get an error like this.

For example, if you added some code setting the .source property of the evolution ArrayCollection, like so:

evolution.source = [1, 2, 3];

then you will get an error at that line telling you that 'evolution' is undefined.

Hope that helps.

It's not telling you that variable i is not defined, it's telling you that the property i is not defined.

I don't think you can run that while loop outside of an actual function. And really, there wouldn't be a reason too. If you need to run that loop immediately you can put it in the initialize function.

Although in the mxml file, you see a lot of xml tags, but when the mxml file is being compiled, it gets translated into a class. Therefore, it's not possible to just write some code in the class that isn't in a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM