Flash Tip: Casting
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.
Hi. My name is Christian Kragh and I'm a freelance Flash designer / developer. Welcome to my blog where I write and hopefully discuss any topics related to freelancing or Flash. I'm currently located in Rochester, NY.









May 6th, 2009 at 7:15 am
Thanks, exactly what I was looking for. Saved me some time.
Bookmarked
May 21st, 2009 at 7:02 pm
This is the best site! got to read more
May 26th, 2009 at 6:16 pm
Hello
Very Nice blog with good ideas !
Very instructive…
Thanks
John
May 29th, 2009 at 2:49 am
Excellent post, keep it up
July 16th, 2009 at 5:40 am
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.