The Making of Evil Clutches, Javascript version.

This is a quick and dirty walk-thru on how I put together the ‘Evil Clutches’ javascript demo. First of all, I bought the book ‘The Game Makers Apprentice‘ and built the first game, in Chapter 2. It’s called “Evil Clutches”, without the “Javascript”. This was a fairly easy game to build with Game Maker and so, after building it and playing it a few times, I took the art resources and modified them a little (made them PNG instead of GIF, removed the animation frames, etc).

After doing this, I started building out my CSS Stylesheet to describe my resources, each resource has it’s own style and all my ‘objects’ use the ‘object’ class (which simply says “position: absolute”.

So, here’s a quick example of some of the CSS:

[css].demon {
width: 130px;
height: 140px;
background-image: url(Demon.png);
}[/css]

My ‘game’ area is defined as a static object, like so:

[css]#game {
position: relative;
margin: 0pt auto;
width: 640px;
height: 480px;
border: 1px solid #fff;
background-image: url(Background.jpg);
overflow: hidden;
}[/css]

Note the ‘overflow: hidden’ to prevent objects from ‘bleeding’ out of my game area, also note the ‘position: relative’ which allows my absolutely positioned children to be offset by the game board. So ‘0,0’ for a child of ‘game’ is the top-left of the ‘game’ element, not the document/window.

Now, I have two other static objects, my Dragon and my Boss, and these were defined as such:

[css]#dragon {
width: 135px;
height: 150px;
background-image: url(Dragon.png);
top: 0px;
left: 10px;
z-index: 20;
}

#boss {
width: 135px;
height: 165px;
background-image: url(Boss.png);
top: 0px;
right: 10px;
z-index: 20;
}[/css]

I added a z-index to these, to ensure they were placed over top of any of my dynamic objects (Demons, Babies and Fireballs), which was done to prevent any ‘bleeding’ overlap when the objects collide.

After I created this style sheet, I started writing some code. First off, I created the ‘JSGameLib‘ side-by-side with this demo, but for the purposes of this walk-thru, we’ll skip the ‘making of’ for JSGameLib.

I added my script references, which included jQuery 1.2.3, jQuery Growl, and JSGameLib. I then added a reference to ‘index.js’, which was just a simple script file i created to hold my games code.

So, let’s take a look at ‘index.js‘.

At the very top, I define a bunch of global variables, this is to help reduce some of the strain on the browser while executing the code repeatedly. Three of the most common things I reference are the Game area, the Dragon and the Boss. As these are static objects that always exist, and only have one instance of each, I simply store a reference to a jQuery object that points to them.

In my $(document).ready() I set some of the jQuery Growl settings, to make it look a little nicer, as well as add the ‘object’ class to all of my ‘game’ children (I did this so I didn’t have to remember to do it when I laid out the Markup on the page).

One of the children in my ‘game’ area is the ‘overlay’, which basically hides my screen and provides basic user instructions. To begin the game, we click this area, so I attached a simple jQuery click handler and it simply calls “gameStart”. I also added a click handler to my ‘Stop Game’ button, which calls “gameOver”.

“gameStart” is pretty simple, it initializes all my global variables to there default values and stores a few pre-calculated values onto my static Dragon, Boss and Game objects. Namely the ‘h’ and ‘w’ properties, so I don’t have to calculate these every time i need to know what they are — this also allows me to quickly resize my artwork and change my game area without having to change -ANY- of my code.

The game ‘starts’ by calling the ‘bossDown’ function, which basically just animates my ‘boss’ object by making it move to the bottom of the screen, this ‘animate’ function has a complete handler, which calls ‘bossUp’ (take a wild guess what ‘bossUp’ does?). These are basic jQuery methods, and you can review the docs for the ‘animate’ function of a jQuery object. The ‘unique’ thing is the simple ‘complete’ handler, which generates a simple ‘loop’ which makes my boss object go ‘up and down’ repeatedly.

I make a call to ‘dragon.controlled()’, and ‘controlled’ is a function available in JSGameLib and I pass in some basic options, enabling support for the UP and DOWN (Arrow Keys and WASD style) as well as a ‘speed’ and ‘duration’ override. For more details, please take a look at the JSGameLib documentation. Following this, I add a ‘keypress’ handler and look for ‘space’ (charCode: 32). If you press ‘space’, I call the ‘fireball’ function. This function simply creates a new DIV element with a class of ‘fireball object’ and then sets the Top and Left CSS attributes to that of the dragons’ current position. I also set ‘display: none’ to make the object hidden initially, and then call ‘animate’ with an ‘opacity: show’ to make the fireball fade in quickly. While it is fading in it also begins to start moving by setting ‘queue: false’ in the ‘animate’ options. I simply tell the fireball to move ‘+=675px’ over the course of 2000 milliseconds. Once ‘complete’, I remove the fireball.

Pretty straight forward so far, eh?

I’m out of time for now, but will finish this up later on — hopefully adding some more detail to what’s written already.

2 comments for “The Making of Evil Clutches, Javascript version.

Leave a Reply