Thursday, December 11, 2008

Flash for Programmers Part 2: The Display List

The Display List in ActionScript 3 is comparable to the DOM on a web page. Through the Display List you can add and remove your View components.

I am not going to go into too much detail here are there are already many excellent articles about it online (http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3/) (http://developer.yahoo.com/flash/articles/display-list.html).

Suffice to say you will be calling addChild() and removeChild() quite frequently.

I am going to give you a few practical tips though...

Often (if you are doing truly modular MVC, which I will cover later), you will be firing off notes that may tell you to add and/or remove various components from the display list. You may fire a note which will ask to remove a child which doesn't exist though and this will cause your app to throw a nasty error.

Just like in real life, I find it handy to name my children i.e.

var dialog:Dialog = new Dialog();
dialog.name = "dialog";
this.addChild(dialog);

Later on, you may have some code which removes it.

this.removeChild(dialog);

However, if you aren't sure it will be there there is a better method

if(this.getChildByName("dialog") != null) {
    this.removeChild(this.getChildByName("dialog"));
}

What you are doing is first checking to see this child exists and if so, you can remove it. This should prevent any potential run time errors.

No comments: