Archive for July, 2008

Creating an Actionscript based AIR project in Flex Builder

The majority of the work I do is pure ActionScript, and doesn’t require the Flex framework. Its inevitable that someday one of these ActionScript projects will need to go local. In the past Zinc would have been the choice, but even the first version of AIR beats it considerably. So what do you do if you have an ActionScript project you want to do in AIR? It turns out that Flex Builder doesn’t have an option to create an ActionScript based AIR project. Luckily I’m not the only person who has run into this problem, there is an entry in the Adobe bug system detailing this exact issue. In the comments, there is a post that details exactly how to set up an ActionScript based AIR project in Flex Builder.

All you need to do is create a new Flex project, and change a file extension right before finishing set up. Once you name the project, click next. After you select the output directory, click next again and in the source path screen change the extension on the main application file from mxml to as.

Change the extension

After the project is created, you need to add code to have the project do anything (even create a window). Since its not done by default, here is some code to create a simple window:


var mainWindow : NativeWindow = new NativeWindow( new NativeWindowInitOptions() );
mainWindow.activate();

Thats it, thanks to Laurenţiu Lozan for the solution!

I freakin love Flint

I finally got a chance to work with Flint for a project I’m working on and its just amazing how well thought out of a particle system it is. With a little time you can take regular boring animations and really put them over the top. With just a few lines of code I was able to add particle trails to the missile and asteroid animations I’m working on:

[SWF]http://www.chrisrebstock.com/stuff/flintexample/FlintExample.swf, 400, 400[/SWF]

If you can’t see the swf above, click here.

The code is really straightforward too. Here is the code for the missile trail particle emitter:


var smoke : Emitter = new Emitter();

smoke.counter = new Steady( 25, 75 );

smoke.addInitializer( new Lifetime( 2, 3 ) );
smoke.addInitializer( new Velocity( new DiscSectorZone( new Point( 0, 0 ), 40, 30, Math.PI * .25, Math.PI * .85 ) ) );
smoke.addInitializer( new SharedImage( new RadialDot( 6 ) ) );

smoke.addAction( new Age( ) );
smoke.addAction( new Move( ) );
smoke.addAction( new LinearDrag( 0.01 ) );
smoke.addAction( new Scale( 1, 10 ) );
smoke.addAction( new Fade( 0.15, 0 ) );
smoke.addAction( new RandomDrift( 12, 12 ) );

smoke.renderer = smokeRenderer;

smoke.x = 75;
smoke.y = 500;
smoke.start( );

It basically boils down to a few simple steps:
– create an emitter
– create a counter for your emitter to determine how many particles are released per second
– create a velocity for particles released, which entails determining a zone for how they are released (check the zones package in the Flint source to see all different types of shapes for how you emit your particles)
– create an image for your individual particles
– add actions to your emitter, which is how you customize the movement, color, style and other aspects of the particles
– create a renderer sprite to hold the particles emitted, which you usually want to be a separate and stationary sprite
– call the start() method on the emitter and animate the emitter if you need to move it

The source code also mentions some key optimizations I plan to take advantage of once I get the time. One important one is limiting the size of your renderer Sprite. You can see in my code I made it the entire size of the screen, its better to only make it the size that you’ll need to show all the particles being emitted.

Here is the full source file.