Archive for May, 2011


Javascript (multiple) inheritance mechanism

May 28, 2011

During the last weeks, I’ve been trying to figure out how I could better write melonJS in order to be more OO oriented (at least in a cleaner way) and to benefit from a simple multiple inheritance mechanism.

I won’t go through all of them in details (there is enough posts on the web about them), but basically I came out with the following possible (and popular) solutions :

  • Prototypal based inheritance (through some helper functions)
  • Closure Based inheritance (trough some helper functions)
  • Using Prototype.js library
  • Using Base2 library
  • Using Motools library
  • Using John Resig Technique (based on Prototype and Base2)

Initially the fight was for me  “Propotypal inheritance versus the world”, as it’s lighter, uses less memory and most of all comes in natural javascript flavor. And with this solution being the most “native” one, it’s also easier to integrate with other libraries.

However, when using inheritance and multiple inheritance a lot (like I’m trying to do), source code can really become a mess, as this solution is also the most verbose one, and some helpers are definitely needed to avoid some annoying issues (like constructors being called with no parameters when extending an Object).

So I’ve been looking around, seeing what was going on, trying to figure out which solution was the most reliable, but also the most popular, made some tests, and finally found this page (among others).

While it was clear for me that using library based inheritance would bring the performance down, I was surprised to see the difference of performances when accessing method ! Of course the test seems a bit old, but seeing the technical explanation behind this result, there is no reason for me it would be different today.

So now I have library based solution that should ease my life with code writing (more OO oriented, and much more clean code) but also giving faster method access. Of course memory footprint is higher, and object creation is slower, but actually who cares (when dealing with games, you just basically create your objects when loading your levels).

At the end the question was : which solution to choose ?

And I finally choosed John Resig one, why ?

  • It’s anyway based on both prototype.js and base2 (so I can assume it brings the best of both)
  • it’s the most compact solution,with only 25 lines of code
  • Very good general feedback, and seems to be working pretty well
  • Seems to give the best compromise in terms of size and perfomances

That’s all guys, however I would be very interested to get your feedback about this, this post is the conclusion of my own analysis on the subject, but if you have any other arguments (or better solutions), I would be very glad to read them as well.

See you soon, in the mean time, I got some work to do 🙂


Alex 4 – the web edition !

May 17, 2011

After weeks of work, I’m very happy today to release Alex the Allegator 4, the HTML5 edition !

Alex4 is a WIP HTML5 remake of the classic “Alex the allegator 4” game, available here and created by Johan Peitz from free lunch design. Alex 4 WE, includes for now the first 6 levels of the original game, and has been developed using melonJS, a free HTML5 game engine.  The library being still in alpha stage, don’t be surprised to find some bugs in Alex4 🙂

Alex4 is hosted at TapJS, allowing to keep track of the highscores. So head over there to try it now !

Alex4 has been completely developed using Tiled, the tilemap editor, as you can also see from the below screenshots :

And finally, melonJS has now a real home, as I opened a small site for it here, so be sure to check it out regulary if you are interested in what will happen next !

Feel free to leave any comments (good or bad) by sending an email at “contact at melonjs dot org”.

Enjoy !