Adobe Flash CS3 Posted by: Dale Franks
on Thursday, January 24, 2008
Yeah, I picked up Adobe Flash CS3, as you can see from the Electoral Vote Counter, and I'm playing around with its odd flavor of Java, called ActionScript 3.0.
I'm actually pretty impressed with it. The new version of ActionScript now allows you to put all of the code on its own layer in the presentation, instead of spreading it all out among the various objects in the movie. It also gives you much fuller—and easier—control over the various drawing objects in the movie.
Some of the conventions are a little irritating at first, however. For instance, all of the mouse events have to be trapped, then sent off to a custom function. In this presentation, all of the states are drawing objects, named with their state abbreviations. So, to tell if you've clicked on a state, you have to catch the mouse click, then send it off to the delegate function, using the syntax:
This line of code tells the program to "listen" for a mouse click on California, and if it "hears" it, to send it off to the delegate function "changeColor".
I'm also not entirely impressed with the variable declaration, because it seems a little odd. In order to create an integer variable called newVotes, you'd use the syntax:
var newVotes:int = 0;
I think this is a bit cumbersome, compared to the same code in, say, C#, which would be:
int newVotes = 0;
I also don't like that the naming conventions for data type names aren't fully standardized. An int is declared as an "int", but a string is declared as a "String". In a case-sensitive language, this is inevitably gonna cause a problem. Make up your mind. Either capitalize your naming conventions or not, but please don't mix them. It's irritating.
For some, the "switch" statement will cause a problem, since cases cannot fall through to one another. At the end of every case, you have to put in a "break;" line. For some of the C-based programmers, this will cause a problem, since they are used letting the cases fall through to the next case. Coming from C#, though, this is how I expect it to work.
Despite the minor irritants, though, ActionScript 3.0 is about 1,000 times better than v2.0. Putting all the code together in one place makes it a LOT more manageable, and the language gives you much greater control over everything on the stage.
I put the electoral vote counter application together in a couple of hours. It would've taken much longer in ActionScript 2.0 to make it work. So, the proof is in the pudding as far as the ease with which you can create applications go.
The extra line of code to do delegation really makes a lot of sense, once you begin working in AS3. It's functionally way different from AS2, but when you begin working in it, you begin to see that it really does allow you to do things much more quickly, all in the same place, with a much greater degree of control than ever before.
I think I'll be able to do some nice things with it.
In case you're a real geek, the code for the Electoral Vote application is below the fold. It's only 318 lines of code, and that includes the blank lines for spacing.
function changeColor(event:MouseEvent):void { var colorInfo:ColorTransform = event.target.transform.colorTransform; var sRedVotes:String = redText.text; var redVotes:int = int(sRedVotes); var sBlueVotes:String = blueText.text; var blueVotes:int = int(sBlueVotes); var sGrayVotes:String = grayText.text; var grayVotes:int = int(sGrayVotes); var newVotes:int = 0;
switch(event.target.name) { case "AL": newVotes = 9; break;
Actually, ActionScript is a variant of javascript, not java. Common error, but preventable. Java is a C++-like compiled language, javascript is an objects-via-prototypes interpreted language with mild syntax resemblance to Java.