Archive for June, 2007

Lorenz spectrum

LorenzSpectrum

A different approach for a spectrum visualizer. This one is based on a lorenz attractor and I really like how fast everything is calculated and gives a feeling of speed. This time I also had to decrease the amount of particles I render. Instead of 64k (which was still running smooth) I decided to use just 8k because it looks better.

What I like about it is that it looks complex but is very simple to do. Based on the spectrum I just change the parameters of the lorenz attractor and that is it. A little bit of paletteMap, BlurFilter, Matrix and ColorMatrix of course as well but it is only about a 100 lines of code.

How to use the Flash library in FlexBuilder

Some people have no clue how they can make use of my favorite Flash CS3 feature so here is a little walkthrough. It is about creating assets inside Flash CS3 and then using them inside FlexBuilder in a very easy way.

I will show you an example how you can easily import a graphic in Flash and then use that graphic in FlexBuilder. First you have to create a new ActionScript 3.0 file. Then press Ctrl+R and import a graphic. Hit Ctrl+L to open the library and right-click on the graphics. Select “Linkage…” and check the “Export for ActionScript” box. Give it a class name. I used “Magneto“. The base class should be BitmapData. That is fine for us. Click on “Ok” and ignore the warning.

Now you have an asset in your library which is a simple graphic. Hit Ctrl+Shift+Alt+S to export and pick a destination of your choice. In the next window make sure to check the “Export SWC” box. This is the most important step.

We are done now with Flash. Open FlexBuilder and create a new ActionScript project. Give it a name and do NOT hit the “Finish” button. Click next and switch to the “Library Path” tab. Click on “Add SWC” and go the the location where you saved your SWF. You should find an SWC file there. Now you may click on “Finish”.

The SWC has been added to your project and embedded. This means it will not be preloaded. I think I could make another post out of that. For now you have now the library of Flash embedded in your FlexBuilder application. Remember the class name I gave my asset in Flash? It was Magneto. If I start typing “new M” I will get syntax completion for that asset. Pretty nice, huh? If I want to show the graphic now I just do this:

[as]var test: Bitmap = new Bitmap( new Magneto( 0, 0 ) );
addChild( test );[/as]

Unfortunately I have no idea what the two parameters are good for. Anyways I hope this helps :)

Lists faster than Array

Since we are planing to release a detailed paper about AS3 optimizations and techniques I was thinking about another optimization for one of the daily problems. Looping through an array that contains objects. The fastest code to do this is

[as]for (;i {
v0 = array[i];
l = v0.length;
}[/as]

v0 is a local variable for my custom class which looks like this

[as]class Vector3D
{
public var x: Number;
public var y: Number;
public var z: Number;

public function Vector3D( x: Number, y: Number, z: Number )
{
this.x = x;
this.y = y;
this.z = z;
}

public function get length(): Number
{
return Math.sqrt( x * x + y * y + z * z );
}
}[/as]

Looks like from a real scenario. You can replace the vector class also with a particle class or whatever. It does not matter. The point is this loop takes 272ms for 1000000 iterations. It is pretty good already but if you add one more parameter to the Vector3D class you can get even faster. This is my modified Vector3D called LVector3D

[as]class LVector3D
{
public var x: Number;
public var y: Number;
public var z: Number;

public var next: LVector3D;

public function LVector3D( x: Number, y: Number, z: Number )
{
this.x = x;
this.y = y;
this.z = z;
}

public function get length(): Number
{
return Math.sqrt( x * x + y * y + z * z );
}
}[/as]

There is a public variable now called next. This variable will always point to the next LVector3D object. The exact same loop takes about 260ms.

[as]var v0: LVector3D = start;
for (;i {
l = v0.length;
v0 = v0.next;
}[/as]

You see how nice this looks? No ugly [] etc. Our variable is directly typed. And we can speed it up even more! If we replace the for(...) part with while( v0 ) it goes down to 249ms. What is also nice about a linked list is that you could add an easy splice functionality or functions to remove elemts from a list. You just have to change the next reference. If you also add a prev variable it becomes even more easy and you could loop backwards.

Building that list of vectors is also very easy. And it looks beautiful. Compare this

[as]for ( var i: int = 0; i < n; ++i )
array[ i ] = new Vector3D( i, i, i );

var v3d: LVector3D = start;

for ( var j: int = 1; j < n; j++ )
v3d = v3d.next = new LVector3D( j, j, j );[/as]

The start variable is defined in the class as private const vector3d: LVector3D = new LVector3D( 0, 0, 0 );. Very nice. Of course not useful in every case but sometimes if you have to squeeze out what is possible here is another method. So stay tuned for our paper.

Run-time file uploads

Very useful. Including source-code for real file uploads with run-time generated content.




Close
E-mail It