AS 3.0 Coming From 2.0 - Article 3

http://www.actionscript.org/resources/articles/738/1/AS-30-Coming-From-20---Article-3/Page1.html

Bryan Grezeszak

Bryan Grezeszak is a freelance AS3 developer and a FlashDen flash component and template author. He is a thorough expert of both ActionScript 2.0 and 3.0, and is more than willing to answer any questions you may have.

By Bryan Grezeszak

Published on March 6, 2008

Another major advantage of typing is error handling. If you have a Number and you try to call gotoAndPlay on it, then Flash will know that you screwed up royally, and will tell you, even giving the exact line where the problem is! The more complex your programs get, the more that can become a very important feature to you, because unless you are perfect (you're not) you will make mistakes. And making it as easy as possible to find those mistakes is very nice.

**Lesson 8: ***Number, int, or uint?*

There seems to be a lot of confusion about this whole idea of Numbers vs. int vs. uint. For those of you who don't know, in AS 2.0 if you had something numeric, it was of the type Number. Simple... but also a bit confining. That was using a lot more processing than necessary for simple things such as keeping a count or looping, since every value was fully capable of having decimals, being positive or negative, etc.

In AS 3.0 this was remedied by adding int and uint. These new types are simpler forms of the type Number. The type int refers to whole numbers (integers) that can be positive or negative. The type uint is whole numbers but no negatives (unsigned integer). The type Number remains just like it was in 2.0, fully math and decimal capable.

For most purposes, a light programmer would only use uint for colors, int for keeping count of something or doing a loop, and Number for most math.

Number uses up the most space, and is the most system intense to work with. But Number also gets used the most, because when doing math (which tends to be done with numeric types), if there is even the chance that decimals will be involved, it needs to be Number, since only the type Number can have decimals.

Type int should be used whenever a whole number is being used and you are doing simple math (+, -, *) with other whole numbers. In other words, situations where you will end up with a whole number and are only working with whole numbers. So that rules out most division.

Type uint is a tricky little bugger to know when to use. uint stands for unsigned integer. What does that mean? Well, a computer can technically only hold positive numbers (since binary is positive 2-base math.) So in order to have negative numbers, we have one bit of the number reserved, and the only thing it does is show whether the number is positive or negative. That bit is called the sign bit. uint doesn't have one, and that is why they can't be negative. But the math in Flash is best done with a sign bit (because math is kind of useless without negative numbers) so uint ends up very slow for standard math operations, because it's getting converted back and forth.

Why have uint then? uint does not have a sign bit, therefore it has one more bit to hold information than int. So while it can't be negative, it can go much higher as a positive (twice as high, actually.) This is especially useful for colors, which are in hexadecimal (16-base math). It's also very useful for working with binary, which AS 3.0 can do.

This has been a tutorial by:

(\___/)

(='.'=) <------ Mad Bunny Skills

(")_(")

Bryan Grezeszak | of Elemental

My first two articles focused mainly on teaching
everybody the changes in syntax from AS 2.0 to AS 3.0. Now I'm hoping
that my audience has worked with 3.0 for a little while since my last
articles, and I'd like to change focus a little bit. I'd like to switch
topics to how to get the most out of AS 3.0. Yes, just by using it,
you're gaining a ton of speed, but there are ways to get more speed out
of it.

AS 3.0 Coming From 2.0 - Article 3

**Lesson 7: Speed Through Types**

First thing first, what on earth is typing? Well, I'm not talking about entering keystrokes on a keyboard. I'm talking about giving variables a defined type. By type, I mean defining whether it's a String, or a Number, or any other different type of object. ActionScript does not require you to give variables a type, so you may ask "Why would we do it then?" Speed. When you make an untyped variable (any variable that you do not specifically give a type) Flash doesn't really define what it is, and has to treat it as if it could be any type. This slows things down a lot. If you tell Flash what it is, then Flash can do what it needs to do with it, and be on it's merry way to the next line of code much faster.

- // this is an untyped variable:
- var slowVar = "myString";
- // by telling Flash that it is a String, it can handle it much faster, this is a typed variable:
- var fastVar:String = "myString";

- // this is an untyped function:
- function slowFunc()
- {
- return 100;
- }
- // by telling Flash that it returns a Number, it can go faster, this is a typed return for a function:
- function fastFunc():Number
- {
- return 100;
- }
- //You can also define that the function will not have a return value using "void":
- function fastFuncWithoutReturn():void
- {
- // lines of code...
- }

Another major advantage of typing is error handling. If you have a Number and you try to call gotoAndPlay on it, then Flash will know that you screwed up royally, and will tell you, even giving the exact line where the problem is! The more complex your programs get, the more that can become a very important feature to you, because unless you are perfect (you're not) you will make mistakes. And making it as easy as possible to find those mistakes is very nice.

There seems to be a lot of confusion about this whole idea of Numbers vs. int vs. uint. For those of you who don't know, in AS 2.0 if you had something numeric, it was of the type Number. Simple... but also a bit confining. That was using a lot more processing than necessary for simple things such as keeping a count or looping, since every value was fully capable of having decimals, being positive or negative, etc.

In AS 3.0 this was remedied by adding int and uint. These new types are simpler forms of the type Number. The type int refers to whole numbers (integers) that can be positive or negative. The type uint is whole numbers but no negatives (unsigned integer). The type Number remains just like it was in 2.0, fully math and decimal capable.

For most purposes, a light programmer would only use uint for colors, int for keeping count of something or doing a loop, and Number for most math.

- // colors should be uint:
- var red:uint = 0xFF0000;
- // most loops should use int, since it is usually only whole number math it will be faster:
- for (var i:int = 0; i < 50; i++)
- {
- doSomethingWith(i);
- }
- // Most normal math should use Number:
- function half(numIn:Number):Number
- {
- var numOut:Number = numIn / 2;
- return numOut;
- }

Number uses up the most space, and is the most system intense to work with. But Number also gets used the most, because when doing math (which tends to be done with numeric types), if there is even the chance that decimals will be involved, it needs to be Number, since only the type Number can have decimals.

Type int should be used whenever a whole number is being used and you are doing simple math (+, -, *) with other whole numbers. In other words, situations where you will end up with a whole number and are only working with whole numbers. So that rules out most division.

Type uint is a tricky little bugger to know when to use. uint stands for unsigned integer. What does that mean? Well, a computer can technically only hold positive numbers (since binary is positive 2-base math.) So in order to have negative numbers, we have one bit of the number reserved, and the only thing it does is show whether the number is positive or negative. That bit is called the sign bit. uint doesn't have one, and that is why they can't be negative. But the math in Flash is best done with a sign bit (because math is kind of useless without negative numbers) so uint ends up very slow for standard math operations, because it's getting converted back and forth.

Why have uint then? uint does not have a sign bit, therefore it has one more bit to hold information than int. So while it can't be negative, it can go much higher as a positive (twice as high, actually.) This is especially useful for colors, which are in hexadecimal (16-base math). It's also very useful for working with binary, which AS 3.0 can do.

This has been a tutorial by:

(\___/)

(='.'=) <------ Mad Bunny Skills

(")_(")

Bryan Grezeszak | of Elemental