Now that we have all the main images set up and ready to go, it's time for the fun part (at least in my opinion) as we bring our virtual world to life with JavaScript and HTML5 canvas. Our first order of business is to create an Actor class which contains properties and methods for both the hero and the bad guys. In other words, both the hero and the bad guys will be instances of the Actor class. The Actor class will be responsible for directing the actors with methods such as moveRight()
and moveLeft()
, and is also responsible for rendering the actors by animating them with sprite sheets.
Follow these steps to create an Actor class which can be used to instantiate the hero or the bad guys:
Define the
Actor
constructor:/* * Actor class should have no knowledge * of the Level or HealthBar classes to * keep it decoupled */ function Actor(config){ this.controller = config.controller; this.normalSpriteSheet = config...