
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Classes and Object-Oriented Programming
|
297
robbers run away from the cops, and the innocent bystanders move randomly, con-
fused and frightened. In the code for this game, suppose we create an object class to
represent each category of person:
function Cop () { ... }
function Robber () { ... }
function Bystander () { ... }
In addition, we create a superclass, Person, from which the classes Cop, Robber, and
Bystander all inherit:
function Person () { ... }
Cop.prototype = new Person();
Robber.prototype = new Person();
Bystander.prototype = new Person();
On each frame of the Flash movie, every person on the screen should move accord-
ing to the rules for his class. To make this happen, we define a move() method on
every object (the move() method is customized for each class):
Person.prototype.move = function () { ... default move behavior ... }
Cop.prototype.move = function () { ... move to chase robber ... }
Robber.prototype.move = function () { ... move to run away from cop ... }
Bystander.prototype.move = function () { ... confused, move randomly ... }
On each frame of the Flash movie, we want every person on the screen to move. To
manage all the people, we create a master array of Person objects. Here’s an example
of how to populate the
persons array:
// Create our cops
var cop1 = new Cop(); ...