Creating a Controller class
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.
How to do it...
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 ...
Get HTML5 Canvas Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.