Creating enemies
For the enemies, we will use OO code too. It will allow us to use inheritance only to specify what changes between the two sorts of enemies. The first kind is slime. Enemies of this type crawl on the ground, and when they die, they flatten and stay where they were killed. They patrol back and forth between two points.
The second kind are flies. They behave exactly like the slimes, but they fly in the sky, and once killed, fall into the abyss.
We will start by writing the code for the slimes. It will be similar in structure to the player's object, only much simpler:
var Slime = function() { this.init = function(div, x1, x2, anim) { this.div = div; this.x1 = x1; this.x2 = x2; this.anim = anim; this.direction = 1; this.speed = 5; this.dead = false; gf.transform(div, {flipH: true}); gf.setAnimation(div, anim.walk); }; this.update = function(){ if(this.dead){ this.dies(); }...