Moving From Flash AS2 To Flash AS3: Numerical Types Rules Of Use

(Note: I’m converting my Flash game “Spin City” from AS2 to AS3 so it can be re-skinned for another project. I’ve decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)

This seems like a very simple topic, and it should be…for anyone who has programmed in language like C or C++. In those languages, numbers have never been handled with a primitive data type named Number. There have always been several types to use for storing number such as int, long int , float etc. However, in Flash AS1, all variables were treated as “variants”. You could store text, numbers, fractions, etc in any variable It was up to the Flash compiler to figure out what you meant any calcuation and do it’s best comply. This led to bizarre situations where, for instance, numbers would be concatenated instead of added together, which then led to confusion and unecessary debugging sessions.

 

Flash AS2

Flash AS2 changed this by adding support for strong typing. If you chose to use it, you could make sure the compiler knew the data type you were using (Number, String, Boolean) and the Flash IDE would yell at you if you used the wrong types in the wrong places. This was helpful for maintaining your code and to root-out some common bugs. However, if you were lazy about their use, you could still leave weakly typed variables lying around and then run into some of the same problems that existed with AS1/

Here is what a few of my Spin City variables looked like in AS2:

[cc lang=”javascript” width=”550″]
private const STATE_INIT:Number =5;
private const STATE_SHOW_TITLE_SCREEN:Number =10;
private const STATE_TITLE_SCREEN_WAIT:Number =20;
private const STATE_SHOW_DIFFICULTY_SCREEN:Number =30;
private const STATE_DIFFICULTY_SCREEN_WAIT:Number =40;
private const STATE_SHOW_INSTRUCTIONS_SCREEN:Number =50;

public static const MAXLEVELSECONDS:Number =150;
public static const CUSTOMER_ARRIVAL_INTERVAL:Number =10;
public static const CUSTOMER_ARRIVAL_LEVEL_MODIFIER:Number =.5;
public static const CUSTOMER_ARRIVAL_LOW_LIMIT:Number =6;
public static const CUSTOMER_MAX_NEEDS_BASE:Number =2;
 
[/cc]

Also, I used variant variables as loop iterators AS2, because, well, I was too lazy to define them as Numbers;

[cc lang=”javascript” width=”550″]
public function clearCars() {
for (var i = cars.length;i >= 0; i ) {
cars[i].removeMovieClip();
cars.splice(i,1);
}
}
[/cc]

This all worked fine, but seemed a bit messy and and confusing. Since variables could be types but did not have to be typed it was very easy to just forget to do it.

Flash AS3

While Flash AS3 still allows for weakly types variables, it has added support for new types beyond Number. these are int and uint. The differences in these types can be defined like this:

  • Number : Used for signed, floating point calculations.
  • int : Used for integer operations for signed and unsigned values
  • uint : Used for integer operations where negative (signed) values are not necessary

From these descriptions, it would seem that uint, which is the most restrictive of all these types, would also offer the most performance benefit. uint is an “unsigned” integer and represents values from 0 to 4,294,967,295 (while int is signed and can only represent values from -2,147,483,647 to 2,147,483,647);

The trouble is, from most reports uint is not very efficient. Check out Grant Skinner’s blog on the subject and also this blog; There have even been some tests that show uints being converted back and forth from Number to int internal in such erratic ways.

Adobe LiveDocs has this to say about unit:

“the uint class is primarily useful for pixel color values (ARGB and RGBA) and other situations where the int data type does not work well. For example, the number 0xFFFFFFFF, which represents the color value white with an alpha value of 255”

Furthermore, it seems that int, while does offer some performance benefit, must be used carefully. As an iterator for a loop or for numerical calculations that will not produce and decimals, int seems to be more efficient.

With that in mind, these are my rules of use for numerical types:

  • Number: The default. You can use this safely for any numerical variables, but get no added performance benefit.
  • int : Use this for numbers that will be used only for addition and subtraction operations, and for loop iterators, and when you need negative integers.
  • uint: Use for color operations that require a full 32-bit number, and for numbers above 2,147,483,647

With these rules applied, here is what my my numerical variables will now look like:

[cc lang=”javascript” width=”550″]
private const STATE_INIT:int =5;
private const STATE_SHOW_TITLE_SCREEN:int =10;
private const STATE_TITLE_SCREEN_WAIT:int =20;
private const STATE_SHOW_DIFFICULTY_SCREEN:int =30;
private const STATE_DIFFICULTY_SCREEN_WAIT:int =40;
private const STATE_SHOW_INSTRUCTIONS_SCREEN:int =50;

public static const MAXLEVELSECONDS:Number =150;
public static const CUSTOMER_ARRIVAL_INTERVAL:Number =10;
public static const CUSTOMER_ARRIVAL_LEVEL_MODIFIER:Number =.5;
public static const CUSTOMER_ARRIVAL_LOW_LIMIT:Number =6;
public static const CUSTOMER_MAX_NEEDS_BASE:int =2;
 
[/cc]

 

All my STATE constants can be of type int because they are never used in any calculations. All my tuning variables are of type Number because they are used in floating point and division operations. I have no use for uint that I can find. However, I can find further use for int in my loops:

 

[cc lang=”javascript” width=”550″]
public function clearCars() {
var carslength:int = cars.length
for (var i:int = carslength;i >= 0; i ) {
cars[i].removeMovieClip();
cars.splice(i,1);
}
}
[/cc]

I can now use int to hold the length of an array, and as the iterator variable for the loop. Both of these uses should improve the speed of the loops.

So, there you have it. Another seemingly insignificant change from AS2 to AS3 that can both improve your code and show some performance benefits, as long as you are careful how you use it.

Leave a Reply