Archive for category Javascript

8WeekGame – I Won!

Woo hoo! I won the very first 8weekgame competition with my HTML5/ Javascript rendition of Manic Miner named “Manic Spaceman”.

Hats off to both Martin & Gareth- I think we all did brilliantly to get a game smashed out in 8 weeks with no previous game development experience. The next competition kicks off in October (the 4th) where I will be joining the other guys to tackle some XNA! We will be free to produce any game we want this time round!

I’m on holiday for the next 3 weeks, returning mid-August when I shall complete the articles detailing the classes I wrote for Manic Spaceman, just incase anyone else wants to try doing something similar and needs some ideas!

No Comments

8WeekGame Competition Over – Vote Now!

The 8 week are over, and each of us has our game up and live for you to try out- pop over here to try them out and vote!

No Comments

Speed up your JavaScript : Video

Found a great video today giving JavaScript performance tips while researching last minute omtimisations for my Manic Spaceman game, being launched tommorow at the end of the 8weekgame competition! Check the video out here of a talk given by Google employee, Nicholas Zakas.

No Comments

Great HTML5 Canvas/ Chrome Demo sites

Found this today; www.chromeexperiments.com loads of cool canvas experiments- geared towards google chrome but most run well in up to date firefox build.

Update: also worth a look in; www.canvasdemos.com

No Comments

ASP.net update panel’s and jQuery

I had a problem today with buttons in an update panel and jQuery- there are some buttons which get hidden or displayed depending on the user’s OS – mac or pc users basically get shown different instructions- However because these buttons are added by a partial post back of the update panel, the jQuery document.ready had already fired before these buttons existed.

So I experimented adding the code to the form.submit event but in the end found the solution on google; I simply added this;

<script type='text/javascript'>
function pageLoad(sender, args)
{
    // your code here..
    showHideMacLinks();
}
</script>

This handles the pageLoad event raised by my update panel which is fired on the initial load and on the partial post back.  You can filter this to only fire on partial post back by adding  “if(args.get_isPartialLoad())”

Found this over on the .net Funda blog

No Comments

Mac Safari Web Developer Tools (inc javascript debugging!)

make sure you don’t have safari running, then go to the terminal window and type;

defaults write com.apple.Safari IncludeDebugMenu 1

You will have a new menu when you launch safari entitled “develop” in safari 5, and “debug” i believe in earlier versions.

No Comments

HTML5 Audio and Video (Media) Tags – How do you know when the data’s loaded? And how to play multiple instances of the same sample at the same time

Quick post with a snippet of code I’ve used for the 8 week game competition I’m in- I’m going to be using html5 audio element for all my game’s sounds and needed a way to make sure the samples were totally loaded so the resource manager knows when to hand control to the game- So after flicking through the html5 spec came up with this code (utilising jQuery to bind my event handler)

Note- This applies to any media object- so the video tag as well, but I’ve only tried this with audio.

// http://www.whatwg.org/specs/web-apps/current-work/#mediaevents
// http://api.jquery.com/bind/
var ao = new Audio();
$(ao).bind('canplaythrough', function() { // totally loaded
alert('ready to play');
});
ao.src = 'sounds/my_sound.wav';
ao.load();

The above example will wait until the media element has enough data to comfortably play through without stopping to buffer. if you wanted you could also bind to a whole host of other events in exactly the same way, which you can read about here. You can check the status at any time to see how loaded the audio/video file is like this;

ao.readyState;

This will return you back a number between 0 (HAVE_NOTHING) to 4 (HAVE_ENOUGH_DATA) – full info can be found here.

Because I’m building a game I have slightly different requirements to the average web project- The way I use this is have a central “ResourceLoader” which I load up with all the url’s to the asset’s I’ll be using, this then reports when all the assets are totally loaded and hands control to the game. So there is a single instance of every image and audio element I’m using- I then hand each loaded sound to the sound manager to play. This raises a problem when i want to play the same sample more than once, overlapping because you are essentially just passing around reference to the same audio object- so i execute the play method, then execute it again and nothing happens because it is already playing! The answer is to perform a deep copy of the audio element before playing- because this is a DOM object you can do this easily like so;

// https://developer.mozilla.org/en/CloneNode
var instance1 = ao.cloneNode(true);
var instance2 = ao.cloneNode(true);
instance1.play();
instance2.play();

This way you can hit the play() method of instance1, and the instance2 and they will both play at the same time, totally independant of each other, even though they both originate from the pre-loaded instance ao.

Anyway- you can see live demos of this here;

Notice the need for 2 demos because chrome cant read wav and mozilla wont do MP3! Ogg vorbis is a common format for the two, but i couldn’t quickly find any example files to use! Read here for browser file format support.

These demo’s were made for us in the 8weekgame site – bare in mind the sound manager class I literally started about an hour ago so it’s got hardly any functionality but it does play sounds so take a look at the source code and hopefully it will all make sense!

No Comments

8 Week Game Competition

I’m currently participating in the 8weekgame competition.  This 8 week sprint is dedicated to (by picking out of a hat) a clone of the retro classic, “Manic Miner“- I’ve chosen to build mine using javascript and the canvas element in html5 (and probably the audio elements also).  The other 2 guys are using Microsoft XNA framework; with Martin targetting windows, and Gareth targetting XBox live arcade.  You can follow our progress here ; 8weekgame.shawson.co.uk

No Comments

OO & Inheritance with Javascript

I’m getting ready to start a game development project and getting my OO Javascript up to scratch- I’ve been googling around for a while as there seems to be a whole load of different ways to implement OO and in particular inheritance in javascript.  I’ve finally found a method that work’s reliably, and with multiple generation of inheritance- It’s from an article on the site point website – for quick reference I will cut to the meat and give a usage example- for details see the original article;

add this function to your project;

function copyPrototype(descendant, parent) {
  var sConstructor = parent.toString();
  var aMatch = sConstructor.match( /\s*function (.*)\(/ );
  if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }
  for (var m in parent.prototype) {
    descendant.prototype[m] = parent.prototype[m];
  }
};

Then define your classes (prototypes) like so;

function AClass(param) {
   this.Text=param;
}
AClass.prototype.Speak = function() {
  alert(this.Text);
}

function BClass(param, name) {
  this.AClass(param);
  this.Name = name;
}
copyPrototype(BClass, AClass);
BClass.prototype.Speak = function() {
  alert('Hi ' + name);

  // Call overridden parent method...
  AClass.prototype.Speak.apply(this);
}

So the above example will give you 2 classes- B is derived from A.  Calling AClass.Speak will give you a single alert with the contents of the ‘text’ property, whereas BClass.Speak will give you two text boxes; the first greeting you by name, and the second being the alert box from AClass.  I’ve tested this myself with multiple generations of inheritance and it worked fine, where other methods have not.

No Comments

Javascript Nintendo Emulator!!

This is crazy; http://benfirshman.com/projects/jsnes/

A full blown nes emulator powered entirely by javascript/ html5 canvas.  I know- I’ve seen quake running, which is a full blown 3d game, and very cool- but this is a full blown games console!

No Comments