Now that we have all of the images and classes for the objects in the game, our next order of business is to build the game engine. Canvas Hero is built with a standard MVC architecture, which separates the data, the presentation, and the control methods. In this recipe, we'll create a Controller class which is responsible for instantiating the model and view, initializing the game, controlling the game state, and managing keyboard events.
Follow these steps to create the controller for Canvas Hero:
Define the
Controller
constructor:/* * Game controller * * The controller is responsible for instantiating * the view and the model, initializing the game, * controlling the game state, and managing keyboard events */ function Controller(canvasId){ this.imageSources = { levelBounds: "img/level_bounds.png", level: "img/level.png", heroSprites: "img/hero_sprites.png", heroHitSprites: "img/hero_hit_sprites.png",...