Bryan Grezeszak does Flash AS 3.0 development at Elemental in Troy MI. 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.
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";
Notice
how I defined the type. Right after the name of the variable, but
before the = sign, I put a colon and then the name of the class that it
is an instance of (String is a class, just like MovieClip or any other
object.) The same rules apply to Number, MovieClip, Object, Array, and
even to functions. By defining what a function returns, or defining
that it returns nothing, it can execute faster!
// 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...
}
Notice
how I typed it, using a colon just like I did for variables, except
after the name AND the parens, but before the opening curly bracket.
Keep in mind that if you give anything a type, it is that type. You
cannot make a String and then turn it into a Number later. Also, if you
define the return value of a function, it must return something of that
type under any circumstance. That means no "if (blah) {return aString;}
else if (blah) {return aNumber}" or anything like that. If you need
something to do that, it cannot be typed. From time to time, you may
see someone declare something *, as in - var myVar:* = blah; This is
the same as it being untyped, except explicitly saying it, so that
everyone knows that that variable is supposed to be that way, and
occasionally it is very useful to do so.
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.
// 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;
}
Contrary
to what it would seem, while uint is the simplest, it is not the best to use whenever possible. To understand why you need a small crash course on
how these numeric types work internally.
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.