package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
/*
The base class for all objects in the game.
*/
public class GameObject
{
// object position
public var position:Point = new Point(0, 0);
// higher zOrder objects are rendered on top of lower ones
public var zOrder:int = 0;
// the bitmap data to display
public var graphics:GraphicsResource = null;
// true if the object is active in the game
public var inuse:Boolean = false;
public function GameObject()
{
}
public function startupGameObject(graphics:GraphicsResource, position:Point, z:int = 0):void
{
if (!inuse)
{
this.graphics = graphics;
this.zOrder = z;
this.position = position.clone();
this.inuse = true;
GameObjectManager.Instance.addGameObject(this);
}
}
public function shutdown():void
{
if (inuse)
{
graphics = null;
inuse = false;
GameObjectManager.Instance.removeGameObject(this);
}
}
public function copyToBackBuffer(db:BitmapData):void
{
db.copyPixels(graphics.bitmap, graphics.bitmap.rect, position, graphics.bitmapAlpha, new Point(0, 0), true);
}
public function enterFrame(dt:Number):void
{
}
public function click(event:MouseEvent):void
{
}
public function mouseDown(event:MouseEvent):void
{
}
public function mouseUp(event:MouseEvent):void
{
}
public function mouseMove(event:MouseEvent):void
{
}
}
}
And the GameObject class also gains the same new 4 functions. These function are empty because, like the enterFrame function, classes that extend GameObject are expected to place their own logic into them.
So now that we have the ability to respond to mouse events lets look at how this is used to create a player ship that will move around the screen.
package
{
import flash.events.*;
import flash.geom.*;
import mx.core.*;
import mx.events.MoveEvent;
public class Player extends GameObject
{
public function Player()
{
}
public function startupPlayer():void
{
startupGameObject(ResourceManager.BrownPlaneGraphics, new Point(Application.application.width / 2, Application.application.height / 2), ZOrders.PlayerZOrder);
}
override public function shutdown():void
{
super.shutdown();
}
override public function enterFrame(dt:Number):void
{
super.enterFrame(dt);
}
override public function mouseMove(event:MouseEvent):void
{
// move player to mouse position
position.x = event.stageX;
position.y = event.stageY;
// keep player on the screen
if (position.x < 0)
position.x = 0;
if (position.x > Application.application.width - graphics.bitmap.width)
position.x = Application.application.width - graphics.bitmap.width;
if (position.y < 0)
position.y = 0;
if (position.y > Application.application.height - graphics.bitmap.height )
position.y = Application.application.height - graphics.bitmap.height ;
}
}
}
In part 3 we created a class called Bounce. It moved in a straight line until hitting the edge of the screen, and which point it bounced back the other way. The Player class is surprisingly similar, except this time instead of moving in a straight line (in the enterFrame function) the Player moves to where the mouse cursor is (in the mouseMove function). And instead of bouncing off the edge of the screen, the Player class moves to the edge and stops.
You may have noticed that startupGameObject makes reference to a ZOrders class. The zOrder defines the depth of the object on the screen, with lower zOrder objects being drawn beneath those with a higher zOrder. The ZOrders class simply holds a few standard zOrder values. This facilitates a level of self documentation: it is immediately obvious what ZOrders.PlayerZOrder refers to, whereas deducing the meaning of a plain value of 10 requires reading the documentation or inspecting the source code directly.
package
{
public final class ZOrders
{
public static const PlayerZOrder:int = 10;
public static const BackgoundZOrder:int = 0;
public static const CloudsBelowZOrder:int = 5;
public static const CloudsAboveZOrder:int = 15;
}
}
By listening to the mouse events we are able to replace the Bounce class with a Player class that moves around with the mouse cursor. Lets now give the player the illusion of flying over the ocean.