package
{
import flash.geom.*;
import mx.core.*;
public class Bounce extends GameObject
{
// movement speed of the bouncing object
protected static const speed:Number = 100;
// direction that the bouncing object should move (1 for right/down, -1 for left/up)
protected var direction:Point = new Point(1, 1);
public function Bounce()
{
super();
}
public function startupBounce():void
{
super.startupGameObject(ResourceManager.BrownPlaneGraphics, new Point(0, 0));
}
override public function shutdown():void
{
super.shutdown();
}
override public function enterFrame(dt:Number):void
{
super.enterFrame(dt);
position.x += direction.x * speed * dt;
position.y += direction.y * speed * dt;
if (position.x >= Application.application.width - graphics.bitmap.width)
direction.x = -1;
else if (position.x <= 0)
direction.x = 1;
if (position.y >= Application.application.height - graphics.bitmap.height)
direction.y = -1;
else if (position.y <= 0)
direction.y = 1;
}
}
}
The Bounce class is an example of a class that extends the base GameObject class to add some specialised logic to game element. In this case that logic is to simply bounce around on the screen.
Bounce has 2 properties. Speed defines how quickly the object will bounce around on the screen, and direction defines the x and y directions that the object is currently moving in.
Once again we have a pair of functions to startup and shutdown the object. I told you they would be a common theme. In this case we simply defer back to the GameObject startup and shutdown functions.
The enterFrame function is where the object updates itself during the render loop. The Bounce object simply moves in a straight line until it hits the edge of the screen, at which point it reverses direction. It’s not very smart, but the Bounce class will not play a part in the final game; its only purpose is as a simple demonstration how a GameObject is extended.
The final two classes that have been created to allow us to start drawing to the screen are the Resourcemanager and GraphicsResource.
Resource management is an important aspect of any game. How will you package the graphics and sound effects used by your game? How do you load them? Where are they stored? These are simple questions that can have big implications. Roughly speaking Flex gives you three options for accessing graphics and sound resources. The first is to load them from the local disk. While this is the traditional way to load resources it is not practical for a game that will be run from the web. For that Flex also allows you to load resources stored on a web server. But while this does eliminate the need to save anything to the end users local drive, it does mean that the game SWF file and the other game resources need to be stored as separate files. Thankfully Flex offers a 3rd option: embedding the resources directly into the final SWF file.
To embed a resource you use the Embed tag. As long as the file your are embedding is recognised by Flex (and that includes a huge range of graphics formats, as well as MP3s for sound) Flex will compile the file directly into the final SWF file, and expose the contents of the file through a class.
The ResourceManager is used as a place to embed and access the game resources. It has no functions at all: it’s only purpose is to expose properties that represent the embedded resources. Here we have defined 1 graphics resource: the brownplane.png image.