O'Reilly logo

HTML5 Game Development by Example : Beginner's Guide - Second Edition by Makzan

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

Time for action – loading the game with levels data

We will refractor our code to support the loading of static ground bodies from a levels data structure. Let's work on it by carrying out the following steps:

  1. Open the box2dcargame.js JavaScript file in a text editor.
  2. We will need each ground setup on each level. Put the following code at the top of the JavaScript file. It is an array of levels. Each level is another array of objects with the position, dimension, and rotation of the static ground body:
    var carGame = { currentLevel: 0 } carGame.levels = new Array(); carGame.levels[0] = [{"type":"car","x":50,"y":210,"fuel":20}, {"type":"box","x":250, "y":270, "width":250, "height":25, "rotation":0}, {"type":"box","x":500,"y":250,"width":65,"height":15, ...

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