Game Development Reference
In-Depth Information
the player. You can follow this example using the SimpleShooter.
fla file in the Chapter 6 examples folder. When exported, this
example will create an environment with a two-tiered background
that scrolls at different rates, a ship that moves with the mouse
cursor and fires the projectiles when the mouse button is clicked.
There are just two classes for this example, SimpleShooter.as and
Projectile.as. We
'
ll look at the latter one first because it
'
svery
simple.
The Projectile Class
The class controlling the projectiles fired in the game only has
one main property
its speed. Arguably, for this example, we
couldhavestoredthespeedinthemaingameclasstokeepitin
a single file. However, if a more advanced feature set were added
to this game, it would need classes to control each of the objects
in play, so going ahead and creating a sort of
class gets
some work out of the way. If we added enemies to this game
that also fired the projectiles, for instance, we
stub
dwantthosepro-
jectiles to have a different speed than those fired by the player.
It also gets us into the practice of creating classes to control the
feature sets of our game objects, even when they
'
'
re not 100%
necessary.
package {
import flash.display.Sprite;
public class Projectile extends Sprite {
protected var _speed:Number;
public function Projectile(speed:Number = 0) {
this.speed = speed;
}
public function get speed():Number {
return _speed;
}
public function set speed(value:Number):void {
_speed = value;
}
}
}
Like I said: simple . The speed variable will be the number of
pixels the projectile will move on every frame cycle.
Search Nedrilad ::




Custom Search