Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Last active December 19, 2015 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourdollars/f168129313679524e01b to your computer and use it in GitHub Desktop.
Save fourdollars/f168129313679524e01b to your computer and use it in GitHub Desktop.
An example from Effective JavaScript
function Scene(context, width, height, images) {
this.context = context;
this.width = width;
this.height = height;
this.images = images;
this.actors = [];
}
Scene.prototype.register = function(actor) {
this.actors.push(actor);
};
Scene.prototype.unregister = function(actor) {
var i = this.actors.indexOf(actor);
if (i >= 0) {
this.actors.splice(i, 1);
}
};
Scene.prototype.draw = function() {
this.context.clearRect(0, 0, this.width, this.height);
for (var a = this.actors, i = 0, n = a.length;
i < n;
i++) {
a[i].draw();
}
};
function Actor(scene, x, y) {
this.scene = scene;
this.x = x;
this.y = y;
this.ActorID = ++Actor.nextID;
scene.register(this);
}
Actor.nextID = 0;
Actor.prototype.moveTo = function() {
this.x = x;
this.y = y;
this.scene.draw();
};
Actor.prototype.exit = function() {
this.scene.unregister(this);
this.scene.draw();
};
Actor.prototype.draw = function() {
var image = this.scene.images[this.type];
this.scene.context.drawImage(image, this.x, this.y);
};
Actor.prototype.width = function() {
return this.scene.images[this.type].width;
};
Actor.prototype.height = function() {
return this.scene.images[this.type].height;
};
function Alien(scene, x, y, direction, speed, strength) {
Actor.call(this, scene, x, y);
this.direction = direction;
this.speed = speed;
this.strength = strength;
this.damage = 0;
this.AlienID = ++Alien.nextID;
}
Alien.nextID = 0;
function SpaceShip(scene, x, y) {
Actor.call(this, scene, x, y);
this.points = 0;
}
SpaceShip.prototype = Object.create(Actor.prototype);
SpaceShip.prototype.type = "spaceship";
SpaceShip.prototype.scorePoint = function() {
this.poinsts++;
};
SpaceShip.prototype.left = function() {
this.moveTo(Math.max(this.x - 10, 0), this.y);
};
SpaceShip.prototype.right = function() {
var maxWidth = this.scene.width - this.width();
this.moveTo(Math.min(this.x + 10, maxWidth), this.y);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment