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

Classes and Document Files Support

As big as the two player class implementations are, they would be much bigger if not for the work done by the supporting classes that create the buttons and dynamic text fields.

Dynamic Output Text Fields

Because all the data input is done with button entries, the text fields for displaying data is all typed as DYNAMIC. Example 13-6 shows the class for the text fields.

Example 13-6. DynamicText.as

package
{
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.display.Sprite;

    public class DynamicText extends Sprite
    {
        private var gameInfo:TextField;
        private var gameFormat:TextFormat;

        public function DynamicText():void
        {
            gameInfo=new TextField();
            addChild(gameInfo);
            gameInfo.border=false;
            gameInfo.background=true;
            gameInfo.type=TextFieldType.DYNAMIC;
            gameInfo.width=70;
            gameInfo.height=20;
            gameFormat=new TextFormat();
            gameFormat.color=0xcc0000;
            gameFormat.font="Verdana";
            gameFormat.size=10;
            gameInfo.defaultTextFormat=gameFormat;
        }
        public function setMove(pMove:String)
        {
            gameInfo.text=pMove;
        }
        public function getMove():String
        {
            return gameInfo.text;
        }
    }
}

The getter/setter functions make it far easier to access and display the available moves. At the same time, the text fields act as temporary storage of values that can be placed into variables or parameters.

Button Controls

The class for buttons is made up of Sprite, TextField and TextFieldType classes. Example 13-7 shows the script for the class.

Example 13-7. MoveButton.as ...

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