Home > Articles > Programming > Games

  • Print
  • + Share This
This chapter is from the book

Recipe: Starting the Game

Now that we’ve created the map, let’s get it running in a browser. We’ll need the .tmx file that we created earlier, along with a copy of the melonJS engine. First, let’s flesh out the index.html file in Listing 5.1.

Listing 5.1. HTML Document Loading JavaScript Files

<!DOCTYPE html>
  <title>Guy's Adventure</title>
<div id="wrapper" style="width: 640px; text-align: center; margin-left:auto; margin-right:auto;">
    <div id="jsapp">
      <script type="text/javascript" src="melon.js"></script>
      <script type="text/javascript" src="resources.js"></script>
      <script type="text/javascript" src="screens.js"></script>
      <script type="text/javascript" src="main.js"></script>

I called my game Guy’s Adventure (my niece named it actually), so I’ve set that as the title. Next, we add some slightly prettier styling to contain the game screen. Now for the tricky part. We make a div with id="jsapp" and inside of it include the melonJS library, a resources.js file, a main.js file, and a screens.js file. We’ll be referring to this div in just a moment.

These files could all be combined into one file, as is the case with other games in this book. That said, it’s useful to know a few different ways to do things, such as how we don’t use a separate JavaScript file in Chapter 4, “Puzzle.” We’re headed in the opposite direction here. So what’s in these files?

The melon.js file is the game engine. We’ll be using a good section of its API in this chapter, but the documentation at http://www.melonjs.org/docs/index.html is absolutely worth a look if you want to have a reference as you’re building. Note that all of the game engine project pages are listed in Appendix C, “Resources.” In case you’re curious, you won’t be making any changes to the engine itself, but like all the engines covered in this book, it is open source. That means if you see some feature missing or a bug you’d like to fix, you can implement it and help make the engine better for yourself and everyone else.

The resources.js file is where you store all your information about what images, audio, and level files (created in Tiled) you need. For now, this file can be simple. All you need is the code in Listing 5.2 to add the resources for the level and sprites you used to build it. Save this as a file called resources.js.

Listing 5.2. Adding a Resources.js File

var resources = [{
  name: "levelSprites",
  type: "image",
  src: "levelSprites.png"
  name: "level1",
  type: "tmx",
  src: "level1.tmx"

The screens.js file is also simple. Think of “screens” as mapping to large game states such as Play, Menu, and GameOver. For now, all you need to do is create a new PlayScreen that inherits from me.ScreenObject and says to load level1 whenever entering the state of being on this screen. Add the code from Listing 5.3 and save it as screens.js.

Listing 5.3. Adding a PlayScreen Object to screens.js

var PlayScreen = me.ScreenObject.extend({!
  onResetEvent: function() {

If you wonder what “me” is, it stands for Melon Engine and provides a namespace for every object in melonJS. Most code that you write and use will not have an object called levelDirector, but for more common words, namespaces are useful to ensure that a name refers to only one object. This is also a good reason to make sure to declare variables using the var keyword. In JavaScript, declaring variables without var creates them in the “global” namespace, meaning that they are accessible from everywhere.

Let’s get back to the code. In Listing 5.4, the main.js file contains your high-level logic and is a bit more complex. First, we create a variable called jsApp. We are using the object pattern here to create two functions. The onload function runs when the window is loaded. Inside of this function, the div with the id of jsapp is declared to be the canvas object you’ll be manipulating throughout the game. It takes four additional parameters for width, height, double buffering, and scale. Because you are using 16x16 sprites, your game is set at a 2.0 scale (zoomed in) compared to the default expectation of melonJS. Because we are using scale, we need to set double buffering to true.

Next, the me.loader.onload function sets the loaded function as the callback function for when the onload function completes. bind(this) ensures that the callback function will have the context of jsApp. The preload function preloads your images and level map from the resources file.

The loaded callback function associates the PlayScreen object that you created in screens.js with the built-in state PLAY (with the game.set function) and then changes the state of the game to PLAY with the state.change function. Finally, the window.onReady() call runs the code jsApp.onload() when the window is loaded.

Listing 5.4. Initializing the App and Loading Assets

var jsApp = {
  onload: function() {
    if (!me.video.init('jsapp', 320, 240, true, 2.0)) {
      alert("html 5 canvas is not supported by this browser.");
    me.loader.onload = this.loaded.bind(this);
  loaded: function() {
    me.state.set(me.state.PLAY, new PlayScreen());
window.onReady(function() {

If you open index.html now, you should see a screen similar to Figure 5.4. It’s a portion of the map that you made. It’s not a game yet, though. What else do we need? Let’s find out in the next recipe.

Figure 5.4

Figure 5.4. Our map loaded in a browser

  • + Share This
  • 🔖 Save To Your Account