Casting in Actionscript 3

I wanted to post something codey again, and this is a topic that can confuse some, so here we go.  This is actually a fairly broad topic, so I’m just going to cover some important things.

1. What is casting?
Casting is basically converting an object that is one type to another type.

2. Why do it?
There are numerous reasons – you’ll see them in the examples.  One main reason is that you have to sometimes.

3. How do you do it?
Good, on to the real meat.  There are to ways to explicitly cast (where you state it in your code) in ActionScript 3.  The first one is by  Type(myObject) .

var mySprite:Sprite = Sprite(randObject);

This takes a value of Type Number (5.5) and changes it to an integer, which is stored as myInt.

Continue reading