Creating the Game Framework

Written by:  • Edited by: Michele McDonough
Updated Jul 20, 2010
• Related Guides: Java | Javascript | Canvas Element

Now that we have seen how the canvas element can be used, it's time to start creating the framework that will be used as the basis of the final game. In part one we look at the first two classes/files that need to be created.

By now we have seen what the canvas element is, how to use it and seen some effects that can only be created using the canvas. Now it's time to start creating a framework that will allow us to use the canvas element to create a simple game.

Before we jump into the code, first take a look at the demo that will be created through the next few articles. On the surface it looks exactly like the demo from the second article, but rest assured that behind the scenes there is quite a bit of work that has been done to create a base framework that will more easily allow us to create the final game.

The code that will be presented uses JavaScript as an object oriented language. Unless you have done a lot of JavaScript programming this may seem odd at first. I recommend that those not familiar with the object oriented capabilities of JavaScript to read this tutorial, hosted by the Mozilla developer centre. It will get you up to speed on some of the programming techniques that will be used here.

Conceptually the framework will be split into two parts: the classes that deal with the underlying 2D engine (i.e. the code that handles the canvas, render loop, input etc), which will be referred to as Engine classes, and the classes that will create the objects that actually make up the game, referred to as Application classes. Since the Application classes are built on top of the Engine classes, we will focus on creating the Engine classes first.

Main.js

If you have studied the code from the previous examples you will see that a lot of that code exists in the Main file. First some global variables are defined. [code]

And, like before, we set the init function to be run once the page has been loaded. [code]

In the init function we create a new instance of the GameObjectManager class. [code]

You will notice that we call the startupGameObjectManager function on the new GameObjectManager instance. The startupClassName function will be a reoccurring theme in this and upcoming articles. These functions effectively serve as the constructor of the class, and this is done for two reasons.

First is that JavaScript doesn't support function overloading (at least not easily). This is a problem when you want to have more than one constructor. By offloading the construction to another set of functions (e.g. startupClassName1, startupClassName2) it is quite easy to define several different ways to construct a class.

The second reason (and this is more of a personal preference) is that I constantly found myself referencing variables from the constructor that were not already defined. This is more a hang over from my own personal experience with languages like C++, Java and C# where the location in the source code where a class variable was defined had no impact on it's visibility in the constructor function. Take the following example in C#.

class Test

{

public void Test() {this.a = 5;}

public int a;

}

This code is legal and works fine. Now take the same example in JavaScript.

function Test()

{

this.a = 5;

var a;

}

The issue here is that the local variable a does not exist at the point where we try to assign the value 5 to it. It will only exist once the code var a; has been run. While this is a fairly contrived example, it does highlight the problem I ran into. However, by keeping the construction of the class in a function like startupClassName, and defining (but not initialising) the local variables in the constructor, I could be certain that the variables I was referencing in these construction functions did actually exist.

Showing page 1 of 3

Comments

Showing all 2 comments
 
Matthew Casperson Aug 23, 2009 7:36 PM
That correct but...
My example was not a good one, but the situation I was trying to avoid was more like

function Test()
{
var b = this.a;
var a;
}

As I mentioned this was more of a personal hangover from other languages where variables existed regardless of the position in the code where it was defined. In C# the variable a would exist even if it was defined below the code that accessed it.

This was a particularly big issue for me because I traditionally define variables at the bottom of the class definition :(
Brook Monroe Aug 23, 2009 11:53 AM
Not quite true....
You offer this script as being incorrect:

function Test()
{
this.a = 5;
var a;
}

In fact, "var a" is redundant, because this.a = 5 creates and assigns the value instantly. The use of the function wrapper creates an object, rather like the following:

var Test = { a : 5 };

Javascript supports creation on assignment; it doesn't support creation on access.

Try this to see the principle in action:


function Item()
{
this.a = 42;

this.getValue = function ()
{
return this.a;
};

return this;


}

var i = new Item();

document.writeln("value stored in i is "+i.getValue().toString());


You will see that the "var a" in function Item() definitely is not required.
 
blog comments powered by Disqus
Email to a friend