AS3 OOP game
tutorial
Written by Stephan Meesters
1.1 Setting up the document class
In the flash authoring tool (Adobe Flash CS3 in this tutorial) you can configure a document class by clicking on the stage and selecting “document class” in properties. In this tutorial we will name our class “GameBasis”.

Figure 2: Setting up the document class
The document class is a extension of the main timeline that we see in our FLA file. In this class we have the direct access to stage which is useful for getting certain properties like stage.stageWidth, stage.quality, stage.frameRate etc. In the game we will want the stage instance to be accessable anywhere. Because the class extends the timeline, we are obliged to extend MovieClip to make it work.
package
{
import flash.display.MovieClip;
public class GameBasis extends MovieClip
{
function GameBasis()
{
// Set stage instances
STAGE = stage;
}
// Static vars
public static var STAGE;
public static var STAGE_WIDTH = 550;
public static var STAGE_HEIGHT = 400;
}
}
Code example 1: The document class in its simplest form [ GameBasis.as ]
In the above code, starting from the top we see that package{ has been used because the class is located at the top-level of our project. If for example a class was placed in the folder “foo”, it would be coded as package foo{.
The constructor function GameBasis() is called whenever a new class is instantiated. We set the value of STAGE as a static variable. A static variable is a property of a class and is accessable from anywhere in the program as GameBasis.STAGE.
Static variables and methods are powerful because classes don’t necessarily have to be instantiated first before we can access them.
Return here to place a comment!

previous page | next page
|