Browse > Home / ActionScript / Flash Tip: Casting

| Subcribe via RSS

Flash Tip: Casting

October 17th, 2008 Posted in ActionScript

Casting can be very useful and in some cases, necessary. AS3 has a system of strict datatyping, much like AS2, but now even more strict. Using casting, you can transform one datatype into another, so to speak. For example, if you are receiving a string in Flash, say “item07″ and you need to use the number from that string, you can extract that using String methods and then cast the string into a uint datatype or Number datatype. Here is a working example of why you would do this, in AS3 code:

var str:String = "item07";
str = str.substr(4);
str += 3;
trace(str); // this will give the result of str being 073

var str:String = "item07";
str = str.substr(4);
var res:uint = uint(str) + 3;
trace(res); // this will give the result of res being 10


The uint(str) is where casting is used. The way you cast a datatype is you wrap it in a different datatype. For example, you can use MovieClip(value), Number(value), String(value), etc. Also, sometimes in situations, I have to use casting to refer to a movie clip in the parent clip, …MovieClip(parent).myMovieClip…
I hope you found this useful, thanks.

5 Responses to “Flash Tip: Casting”

  1. Musco Says:

    Thanks, exactly what I was looking for. Saved me some time. :)

    Bookmarked


  2. Oler Says:

    This is the best site! got to read more


  3. Prophet Says:

    Hello
    Very Nice blog with good ideas !
    Very instructive…
    Thanks
    John


  4. Mccurtain Says:

    Excellent post, keep it up


  5. Hildreth Says:

    Thank You for that post! It’s been a long day and your info just set me right. Now I can see what other good info is out there. Cheers.


Leave a Reply

Security Code: