About Flash Optimization

Working with flash and actionscript I often stumble upon bad and slow code (either old code of mine or written by others – designers); In this post I’ll try to list some resources worth knowing if you want to write fast and nice code.

Best Practices

  1. Do not use Objects, if you know which properties will be finally involved: dynamic properties are slow to read. Create custom classes instead.
  2. Strong type everything. If you think there is a reason to avoid strong typing, at least use * as type.
  3. Try out and test different strategies with Grant Skinner’s performance framework.

Resources

Feel free to suggest more :-)

Quickreference: flipping DisplayObjects

This one flips DisplayObjects on a vertical or horizontal axe.

public static function flipHorizontal(obj:DisplayObject):void {
    var m:Matrix = obj.transform.matrix;
    m.transformPoint(new Point(obj.width * .5, obj.height * .5));
    m.tx = (m.a > 0)?obj.width + obj.x:obj.x - obj.width;         
    m.a = -1 * m.a;
    obj.transform.matrix = m;
}
       
public static function flipVertical(obj:DisplayObject):void {
    var m:Matrix = obj.transform.matrix;
    m.transformPoint(new Point(obj.width * .5, obj.height * .5));
    m.ty = (m.d > 0)?obj.y + obj.height:obj.y - obj.height;
    m.d = -1 * m.d;
    obj.transform.matrix = m;
}

WordPress widget: TagPosts

I’ve written a small wordpress widget, to show in the sidebar all the posts from a given tag, or from the tag archive page you are visiting, or both.

You can see in action in this blog, if you select any tag from the sidebar in homepage.

I’ve taken inspiration from the Category Posts Widget 3.0 by James Lao.

You can download the plugin from the wordpress repository.

Intense Minimalism

One of the most promising guy I’m pleased to know, Davide ‘Folletto’ Casali, has just updated his domain and his blog, to intenseminimalism.com. To describe what he’s into he uses a nice formula: Hybrid Interaction Designer = Design × Psychology × Technology /  Simplicity × Complexity.

His work is really inspiring: simplicity and minimalism together are really a great tools to lay down and explain complex thoughts.

Sealed and dynamic classes

Back in the days of actionscript 2, you could add any property to pretty much any object, just calling it and setting a value, like this:

 myMovieClip.thePropertyILikeWithAStrangeName = "some string"

Actionscript 3.0 instead, being a more serious language, defines the basic class as sealed. That means that you can’t add any property on a object. You have to extends that class, and adding public properties.

public class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

That class is sealed. You can’t

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

The dynamic keyword, permits to ‘unseal’ a class, giving back the possibility to add any property on runtime.

public dynamic class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

Now you can:

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

There are speed and cleaness drawbacks on using dynamic classes. The rule is that if you use it, you should be able to seriously motivate it.

Note that there are few dynamic classes in the default Actionscript framework; the most known is of course, Object .

Read more:
livedocs
ActionScript 3: Dynamic Classes