O'Reilly logo

Building HTML5 Games with ImpactJS by Jesse Freeman

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

Modules

Impact’s source code is organized into modules. Since JavaScript itself does not have an include() function that can load other JavaScript source files into an object, Impact has its own system. A module typically looks like this:

1    ig.module(
2        'game.my-file'
3    )
4    .requires(
5        'impact.game',
6        'impact.image',
7        'game.other-file'
8    )
9    .defines(function(){
10        // code for this module
11    });

The first block defines the module name 'game.my-file', which directly corresponds to the file name. Modules and their dependencies typically reside in the lib/ folder of your Impact project directory, and subdirectories are included in a path to these files using object-model dot syntax. Therefore, the my-file.js file sits in the lib/game/my-file.js.

The second block defines any additional files that will be loaded at runtime. Since JavaScript itself does not have an established way to load other JavaScript source files into an object, Impact has its own system. The modules listed in the .requires() method will be loaded from the lib/impact/game.js, lib/impact/image.js, and lib/game/other-file.js project directory, respectively. These required files will be loaded before the module’s body and before the last block of the above module example is executed.

The last step the module takes is to execute the function passed to the .defines() method. This linear process allows you to control when code is loaded and run. It’s important to follow Impact’s file naming and location structure since it will try to automatically load these resources for you during the pre-load phase. Next, we’ll talk a little more about classes in Impact and how they work.

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