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:
- Open the
box2dcargame.js
JavaScript file in a text editor. - 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, ...
Get HTML5 Game Development by Example : Beginner's Guide - Second Edition 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.