SimpleAS3: An ActionScript Framework for Designers, Animators, and Part-time Coders
Recently, Colin Moock, author of Essential ActionScript 3.0 and a well-respected leader in the Flash community, presented The Charges Against ActionScript 3.0. A few of the problems he describes apply to everyone who targets Flash Player, such as the difficulty even hardcore developers seem to face when dealing with the garbage collector. However, many of the issues come from the perspective of designers, animators, and other part-time coders. These soldiers of Flash Player have been building rich content and thriving for years, but many are frustrated with the myriad of changes ActionScript 3.0 brings to the table.
AS3 is more like traditional programming languages, and it has a stronger focus on object-oriented programming (OOP). Many users preferred the more flexible scripting focus of earlier versions of this language, and some find OOP much more difficult to grasp. On the other hand, a lot of folks have simply become frustrated that AS3 often requires much more code for things that were easy in AS2. With my new framework, SimpleAS3, I hope to address many of these issues and give the "everyday Flasher" (as Moock names them) a way to use AS3 in a more familiar and flexible manner.
Below, I'll cover a few specific things that SimpleAS3 makes easier. Be sure to read the SimpleAS3 documentation to learn everything that this library can do because I can't possibly describe it all in one blog post.
ActionScript 3.0 is Strict
The AS3 compiler is very strict compared to AS2. For instance, since the parent property on display objects is typed as DisplayObjectContainer, developers must often cast it as a different type to get around the fact that DisplayObjectContainer is sealed. This makes a very common method of communicating between MovieClips in AS2 far more difficult in AS3.
SimpleAS3 simply turns off AS3's Strict Mode. This compiler feature is fully supported by Flash CS3, but many people who might want to use it don't know it even exists. With Strict Mode off, Flash CS3 is much more accepting of dynamic property access, so it makes AS3 coding feel much less restricted. On the downside, just like AS2, simple typos can lead to errors that might be difficult to find. However, since many AS2 coders are used to that already, it's often an acceptable tradeoff.
Listening to Events
In some ways, events in AS2 were a bit easier to use. In others, AS3 made some very useful improvements. While developing SimpleAS3, I wanted to keep the more powerful event system of AS3 intact while making it look a bit more like AS2. What I ended up with is a little bit different than both, but certainly not too difficult to grasp.
-
button.onClick(function()
-
{
-
doSomething();
-
});
As you can see, adding an event listener is a little faster, since I brought back the old onEventName syntax. This was achieved by adding the event functions to class prototypes. Yes, those still exist in AS3, which may surprise some people. Additionally, for many events to which you don't intend to stop listening, it's often easier to simply use an anonymous event handler function. You'll also notice that I ignored the event parameter. Thanks to Strict Mode being turned off, function arguments become a bit more flexible.
If I wanted to follow best practices a bit more closely, I might consider rewriting the code above to look something like this:
-
button.onClick(clickHandler);
-
function clickHandler(event:MouseEvent):void
-
{
-
doSomething();
-
}
Though I use regular AS3 in my own projects, I enjoyed playing with the event shortcuts so much while I was testing SimpleAS3 that I sometimes have to stop myself from typing onClick() instead of making a proper addEventListener() call.
Loading Assets at Runtime
SimpleAS3 makes loading things like SWFs, images, and XML files at runtime very easy. For images and SWFs, a new function named loadChild() has been added to all display objects. In many ways, it is similar to loadMovie() in AS2. In one function call, you pass in a URL, set a name, and optionally set up listeners for the complete, progress, and IO error events. A Loader is automatically created for you and added to the display list.
-
this.loadChild("http://www.example.com/images/logo.png", "logo",
-
completeHandler, progressHandler, errorHandler);
Similarly, a new global function loadXML() makes it easy to read an XML file. Again, it's just one function call.
-
loadXML("http://www.example.com/data/config.xml", completeHandler);
-
-
function completeHandler(data:XML):void
-
{
-
//access data using E4X right here
-
}
In future versions, I hope to add more shortcut functions for loading assets at runtime. I'd love to hear what sorts of data people load most commonly and how they think it could be easier.
I have a feeling this framework may be a bit controversial, especially since my regular audience on this blog is developer-centric. However, like Colin Moock, I think that the part-time coder is an important member of Flash Player's target audience, and I want to see these folks continue to thrive on the types of projects they enjoy creating. Those aren't necessarily the same sort of projects developers like me build, so it makes sense that our tools may be a bit different. In other words, not everyone needs the power of the Flex framework nor all the best practices that should be followed when building enterprise-scale RIAs.
I encourage you to leave feedback in the comments below, or to comment on the Future Plans section of the Wiki I've set up on Google Code. I'm planning to get a better support and feedback system set up soon, but this should give everyone a good place to start if they have questions or comments. Of course, if you're interested in learning more about SimpleAS3, please check out the documentation and download SimpleAS3 to give it a try. The MXP installer requires Adobe Extension Manager.
Note: I'll be on vacation over the weekend and speaking at 360Flex San Jose next week, so I may not be able to respond to or approve comments immediately. I'll try to address issues and questions as quickly as my schedule permits during this time. I love feedback, especially on an experimental project like this one, so please share any thoughts you have.
Like what you just read? Follow @joshtynjala on Twitter.








8 Comments
Looks really impressive. This is definitely something that is needed for not only less-code-centric folks, but also rapid prototyping. I look forward to playing with this.
Thanks!
Yeah, thanks for mentioning prototyping PJ. I focus a lot on the part-time coders and AS2 world in my descriptions, but prototyping is something I definitely considered while I was planning SimpleAS3. If you want to write less code for proof-of-concepts and other projects of that type, SimpleAS3 can certainly make your job easier.
Great idea,
Any chance of making the classes available for use with Flash Develop?
[...] SimpleAS3: An ActionScript Framework for Designers, Animators, and Part-time Coders by Josh Tynjala [...]
Will, that might be a possibility. I’ve been using Flex Builder to test the framework, and SimpleAS3 is compatible with the Flex compilers that FlashDevelop uses. The Flash CS3 extension that automates project creation is a big part of making SimpleAS3 easy. However, I’m not against creating a download that is just the source code and adding a tutorial about setting up the framework manually. It may not be right away, but I’ll keep it in mind.
Looking great but I’m getting an error. When I create a new project (test), an alert pops up; it reads “While executing in test.fla a Javascript error occured”
Any idea what caused this ?
Thanks.
Anggie, it seems that there was a JSFL error while the project was being created. Can you provide me with any more information? Did the output panel include a line number or any other hints as to where the problem occurred?
this great !!! would be great is really MVC setup as well