Creating a Model class

In this recipe, we'll create a Model class which is responsible for initializing and updating the hero, the bad guys, the level, and the health bar. These objects can be seen as the "data" of our game.

How to do it...

Follow these steps to create the model for Canvas Hero:

  1. Define the Model constructor:
    /*
     * Game model
     * 
     * The model is responsible for initializing and
     * updating the hero, level, bad guys, and health bar
     */
    function Model(controller){
        this.controller = controller;
        this.healthBar = null;
        this.hero = null;
        this.level = null;
        this.badGuys = []; // array of bad guys
        this.heroCanvasPos = {};
    }
  2. Define the removeDefeatedBadGuys() method which loops through the bad guy array and then removes the ones that are no longer ...

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.