O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

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

A Functional Namespace Example

Let's put our new knowledge of namespaces to work in a simplified program. Example 17-1 contains the beginnings of an application we'll develop over the coming sections—a child's word-recognition game. In the game, the player is shown a picture of either a color or a fruit, and asked to choose its name from a list of options. Figure 17-1 depicts two different screens in the game.

A child's learn-to-read game

Figure 17-1. A child's learn-to-read game

For now, each item in the game will be represented by a variable whose value is a string description. We'll define the catalog of all item variables in a class called Items.Using namespaces, we'll separate the "fruit" variables from the "color" variables; fruit variable names will be qualified by the fruit namespace, and color variable names will be qualified by the color namespace.

Take a look at the code in Example 17-1, then we'll review it.

Example 17-1. Kids Game: a functional namespace example

// File fruit.as
package {
  namespace fruit = "http://www.example.com/games/kidsgame/fruit";
}

// File color.as
package {
  namespace color = "http://www.example.com/games/kidsgame/color";
}

// File Items.as
package {
  public class Items {
    fruit var orange:String = "Round citrus fruit";
    color var orange:String = "Color obtained by mixing red and yellow";

    public function Items () {
      trace(fruit::orange);
      trace(color::orange);
    }
  }
}

In the preceding code, ...

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