Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.


Flex SDK 3 doesn't like arithmetic in member variable definitions [Updated]

Posted February 10, 2009 by senocular

Update: It was recently pointed out to me that the arithmetic was not what was throwing off Flex, it was the types of values I was using. ActionScript does not discern between integers and floats at the primitive level; everything is a Number. But since 1/20 was being interpreted by the compiler as integers, it was being evaluated as int(1/20) which became 0 in the final code. Changing the 1/20 to a 1.0/20.0 would have also fixed this!

This one took me my morning to figure out: you cannot use arithmetic (such as 1/20) for member variable definition values in Flex 3. You can in Flash CS3; you can in Flash CS4; and you can even with Flex 4. But you just can't in Flex 3. This all started with someone having a problem with my SWFReader class.
I was sent an email indicating that the SWFReader class was not properly reading the dimensions of a SWF. My first thought was that this was the result of an incomplete readRect method in the reader - the function used to determine a SWF's dimensions.

I had initially created the function to just handle a SWF's dimensions without regard to other RECT structs in the file. If I remember right, the SWF dimensions were "easy" in the sense that they were saved in a RECT which always had an x and y of 0. The size of the SWF was simply the size of the RECT. I had since revamped that method to be more robust and thought this issue could somehow be resolved by using the new version of the class (which includes some other new features as well, such as a callback for reading SWF tags). My quick fix, post the updated class and send it off in my response.

No good. It didn't work. At that point I actually had to do some work. I fired up a sample FLA I had using the class and tested it out. No problems. I even requested the SWFs used when the failure was found and they worked fine for me. Different browsers, different SWF versions, different players... it was all working fine - for me.

So then I needed more information still - OS, compiler, testing environment, etc. It all ended up being pretty much the same that I had already been using except Flex 3. So I fired her up, created a test, and launched in debug mode. Fail. What?!?

I ran through all the bit/byte functions and after a lot of tinkering found that they were all producing the same data in both the working and non working versions. I knew somehow Flex was to blame, but where? It seemed like the Rectangle I was creating simply came up with a size of 0 with no explanation.

But then I thought I'd take a chance and see what the value of the TWIPS_TO_PIXELS constant was. I mean, it's just a constant, how can it be wrong? Its set once and you're done with it. Sure enough, it was coming up with a value of 0. D'oh! The reason: it was defined as 1/20 rather than a single primitive value. No go. After changing it to .05 all is well.

Needless to say, the SWFReader class has been updated. Also, despite losing my morning, I've learned something new, and that's always a plus :)