Game Development Reference
In-Depth Information
Creating sprites that move on a vector
We now need to create a set of game objects we will call static sprites. While these sprites might
move, they have only one frame of animation. The Shot , Enemy , BonusPlane , and Ship objects in
our game are all static sprites, and they are all defined in a similar way.
Defining a sprite that moves between two points
Here is a question for you. What is one very basic thing that you can do if you have two points?
Well, I'm sure there are many, but the most important one we can do is: draw a line . This might
not seem spectacular right now, but with a line we can determine the distance between two
points. If we know the distance between two points, and if we know the velocity (speed) and
direction ( vector ) that something will travel at along that line, we can figure out how much to move
an object each frame in Flash.
The good news is that we can determine all of this on the fly and make sprites move from point to
point. Let's start by looking at the very basic design of our Shot class.
Shot
The first sprite we will look at is Shot . This sprite represents the shell fired by the player when the
mouse button is clicked. Shots always start at the bottom center of the screen, and they always
travel to the center of the crosshairs . This means that we will always have two points to work with.
This is very important, because these two points are going to make moving our sprites very easy.
First, we must import the classes necessary to work with Bitmap images and BitmapData , plus the
Sprite base class.
package com.efg.games.flakcannon
{
import flash.display.Shape;
import flash.display.Sprite
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
public class Shot extends Sprite
{
Then, we must define all the variables we will need in the class. First, we need variables to hold
the ShotGif() image, and the BitmapData associated with it. Alternatively, you could create Shot
as a MovieClip in the Flash IDE and drag the bitmap image into it, but we are going to focus on
doing this programmatically so the code will be portable to other tools. We will use image to hold
the reference to the ShotGif instance, and imageBitmapData to hold a reference to its BitmapData .
public var imageBitmapData:BitmapData;
public var image:Bitmap;
Next, we need to define variables to hold the two points we will use to calculate the movement
vector for the Shot . The easiest and cleanest way to do this is to create a Point object to hold
each set. We will name these Point classes startLocation and endLocation .
private var startLocation:Point;
private var endLocation:Point;
Search WWH ::




Custom Search