O'Reilly logo

HTML5 Canvas Cookbook by Eric Rowell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required