O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: List Display Adapter

Adapter patterns also come in handy to create reusable classes that use existing classes to implement some of the required functionality. For example, think about when you would want to display a list of values on the Stage. There may be many instances when you want to do this: lists of names, lists of high scores in a game, lists of products in a shopping cart, etc. In Flash you can use a TextField to display a list, putting each item on a separate line. However, this requires that the display text be preformatted with a carriage return character ('\r', ASCII 13) separating each list item before sending it out to the TextField object for display. The following code snippet will display a list using a TextField object.

var listField = new TextField();
listField.x = 20;
listField.y = 20;

var sList:String = "Bread";
sList += "\r" + "Butter";
sList += "\r" + "Cheese";

listField.text = sList;

addChild(listField);

This works, but what if you want to delete an item from the list? You essentially have to recreate the list again. Thinking more about this, it becomes apparent that holding the list items in an array and splitting the array to create a string separated by return characters would be a better option. This would require more code, and our little code block ceases to become a "snippet" and screams out for a generic solution.

Wouldn't it be nice to have a reusable ListDisplayField where you can add one list item at a time? It would function very much ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required