HTML and CSS Reference
In-Depth Information
You create a variety of different enemies in the section “Setting Up the Enemies” by setting different vari-
ations of parameters.
In a production game, if you don't want to worry about handling the math yourself, you could consider using
a tweening engine such as TweenJS ( ) , which can handle smoothly moving objects
from one position to another in a number of interesting manners.
Constructing the Enemy Object
You can create enemies from a blueprint that sets the sprite image used, the initial starting location, and the
values for the movement of constants A-H. The constructor also enables an override object to be passed in to
override the default blueprint settings.
Much like PlayerMissile , the Enemy object adds methods onto the prototype to speed object creation
and reduce the memory footprint.
This initial version of Enemy looks much like the previous two sprite classes that have been built ( Player-
Ship and PlayerMissile ), with a constructor function shown in in Listing 2-2 that initializes some state;
a step method that updates the position and checks if the sprite is out of bounds; and a draw function that
renders the sprite. Because of the need to copy over from the blueprint and any override parameters and set up
the velocity equation parameters, the constructor function is a little more complicated than previous ones.
JavaScript doesn't have a built-in method to easily copy attributes over from another object, so you need to
roll your own loop over the attributes to do it. To prevent the need for the blueprint to set each of the parameters
A-H, each of those are also be initialized to zero.
Listing 2-2: The Enemy Constructor
var Enemy = function(blueprint,override) {
var baseParameters = { A: 0, B: 0, C: 0, D: 0,
E: 0, F: 0, G: 0, H: 0 }
// Set all the base parameters to 0
for (var prop in baseParameters) {
this[prop] = baseParameters[prop];
// Copy of all the attributes from the blueprint
for (prop in blueprint) {
this[prop] = blueprint[prop];
// Copy of all the attributes from the override, if present
if(override) {
for (prop in override) {
this[prop] = override[prop];
this.w =[this.sprite].w;
this.h =[this.sprite].h;
this.t = 0;
Search WWH ::

Custom Search