Sunday, April 13, 2008

Reflection in ActionScript 3

I'm not a big fan of the old eval() function in earlier versions of ActionScript. As of ActionScript 3, it is no longer supported. For the most part, that is a very good thing. My buddy, Jason, is migrating from AS2 to AS3 and he found himself looking for the eval() function. He came to me and asked me about it, I replied "you mean the evil() function?".

The problem with eval() is that you were executing code that was being created at runtime, meaning there's no chance of the complier helping you catch errors. He showed me the code he was working on and --usually-- you can solve the lack of eval() through a better architecture. As I looked at his program, I realized he had a pretty elegant solution. Re-architecting would take a good deal of time and not necessarily yield as elegant result. Java has quite a few classes for dealing with reflection, so I figured (given how similar AS3 is to Java) there must be some similar classes. As it turns out, the pickings are pretty minimal... but there were enough to come up with a nice, dynamic solution to his problem.

In the flash.utils package, there are a number of public functions. The one we were looking for (before we even knew it) was getDefinitionByName(). This function takes a string that is the name of a class, an returns the class object for it. With the class object, you can then create an instance of the class. Here's a simple example you could run in the Flash IDE:
var myClass:Object = getDefinitionByName("Symbol1");
addChild( new myClass() );
It assumes you have a Symbol named "Symbol1" that is exported for ActionScript. If you are doing this in Flex or inside an external class file, remember to import the flash.utils package. The danger of runtime errors still exists, but it is more limited now in that you can create classes and not just execute and arbitrary chunk code on the fly like you could with eval().

Audio Display List?

The Flash 9 player using ActionScript 3 (for Flex or Flash), has an internal structure called a display list. Items in the display list, will be shown, items not in it will not be shown. So if you load an external SWF, it will not be visible until you add it to the display list. Once it is in the display list, it'll receive events (like MouseEvents, KeyboardEvents, ENTER_FRAME, and so on). That's nice, because you can load it and display it at will.

But what about a SWF that also contains audio? As it turns out, there is no audio equivalent to the display list. Once a SWF containing audio is loaded, it will begin to play and you'll hear the audio, whether or not you're displaying it. To stop the audio, listen for Event.INIT on the loaderInfo in the Loader object. Event.INIT fires when the first frame is loaded so you can call stop() it to prevent the playback head from moving forward in the timeline.

Something I love about open source

I was just re-reading through the Blender release logs and for version 2.45 (the current one):

The 2.45 is a bugfix release, to stabilize the 2.4x series.
No new feature have been added, but serious effort has been put in tracking bugs and fixing them. Some performance issues have also been addressed.

Years ago, when I was used Director daily, I don't know how many times I read posts crying for a release that just addressed bugs. Skip new features. I guess the business folks at Macromedia (now Adobe) couldn't makes sense of the importance of stability to users. Blender is one of the most stable pieces of software I use. When you're trying to get a job done, stability is paramount.

Tuesday, April 01, 2008

Blender Basics 2

Blender Basics 2 was released today by CartoonSmart (no foolin')! BB2 is 4 hours (twice as long as Blender Basics!). In BB2 you learn how to:
  • Use procedural textures on materials
  • Place an image using UV maps
  • Use the Screw tool to create corkscrew extrusions (like springs)
  • Use multiple textures
  • Extrude along 2d and 3d bezier paths
  • Lathe using the Spin tool
  • Create a basic rig (a skeleton)
  • Skin a rig 3 different ways
  • Use boolean modifiers on objects
  • Use layers
  • Use groups
  • Append from other files
  • Create new screens
  • and more tips on working with materials, meshes, objects, lights, and rendering
BB2 assumes you are familiar with navigating around the 3d workspace. So it is appropriate for users who have seen Blender Basics!, either the full version or the free version (which is the first 40-minutes of Blender Basics!), or if you are already familiar with some of the basics this video should be fine for you. It is still going through the many features of Blender through the creation of the scene pictured above.

The video high quality, it is compressed using the H.264 codec at Best quality, Millions, 1024x768. The audio is AAC 44.100kHz. The only place to get it (legally) is Just as we did for Blender Basics!, and I will be donating a percentage of the profits to the Blender Foundation to support further development of Blender.