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.
You will need Flash Player 10.2 to view this!
Since FP10.2 is out, I thought I’d make a custom cursors demo. It relies on two outside libraries, MinimalComps and as3Gif. The project files are here! The demo demonstrates a static cursor, an animated one, and one you can draw in the program.
Many people still use Arrays when programming in Actionscript 3 when they should be using Vectors, so I’m going to give a brief synopsis of Vectors, especially their various constructors. Just as a warning, vectors are normally thought of as a math or geometric thing, a magnitude and direction. In Actionscript, they are not. If you are looking for this functionality either use Point for basic 2d stuff (or create your own Vector2D), or Vector3D for 3D geometric vectors. Why Flash named two totally different classes Vector and Vector3D is beyond me.
On to the Vector class in flash. A Vector is just a typed array – an array where all the objects in it are the same type. The benefits of using vectors include faster performance (major benefit) and not having to cast objects.
The possibilities for working with sound in ActionScript have been expanded with the relatively new SampleData event: you can now record sound from the microphone into a ByteArray or play sound dynamically from a ByteArray. This opens up the potential for all kinds of cool things, and I came across one example of this from Kelvin Luck, a class that plays back sounds at different speeds.
Also cool is the Google Translate text to speech webservice, which can be used to retrieve an mp3 file from a text query. Pete Shand circumvents the 100-character limit with his TextToSpeech class.
Combining these two I made a cool little application (just type in text, and press Hear):
Vodpod videos no longer available.
(If the swf doesn’t load, try here.)
There are a lot of useful ActionScript libraries out there, and almost all of them are free. Here are my top 5:
- TweenLite: A easy to use tweening (animating) engine for about anything. This is a must-have for many me and many others. Also at the website are tools for organizing your tweens and loading media.
- Minimal Comps: Need quick and easy components and don’t want to use Flash CS? This provides all the basic ones components along with a couple unusual ones.
- as3corelib: Can’t forget the practical as3corelib, which add a lot of functionality to actionscript. Just be wary, the dict protocols won’t work in any online flash project because of security restrictions.
- Box2D: A powerful physics engine. this one has a little learning curve, so here is an okay tutorial and a better one that is slightly outdated.
- Graffiti: A drawing library with multiple tools and controls
Honorable Mention: Squiggly: A spellchecking tool that can check individual words or, in flex, check entered words real-time.