title | authors | intro | types | categories | published | updated | status | |||
---|---|---|---|---|---|---|---|---|---|---|
Cut the Rope now in HTML5 |
|
Cut the rope was a game originally built for iPhon... |
|
|
2012/01/09 12:00:00 |
2012/01/09 13:00:00 |
archived |
Cut the rope was a game originally built for iPhone and Android by ZeptoLab and it's gone on to sell 70 million copies, the good news is that it has been ported to HTML5 by PixelLabs, ZentoLab and the Internet Explorer team. It works in Chrome, Safari, Internet Explorer 9 and Opera.
Play Cut the rope Read the developers guide
The game was ported from Objective-C to JavaScript, which must have been a huge challenge given the fact Objective-C and JavaScript are very different beasts.
Objective-C is an object-oriented programming language and as such has inheritance and classes where as JavaScript is a dynamic scripting language. You can create 'class like' objects in JavaScript using prototypes but, It's harder to get native like performance out of JavaScript. The game is proof, however, that with a little bit of creative thinking, modern JavaScript engines can deliver the goods.
In the article that accompanies the game, the developers talk about the mind-set switch required to move from Objective-C to JavaScript and pointed out two areas where a different approach had to be taken to get the game running smoothly.
In the Objective-C application there were numerous instances where a procedure would call itself again and again passing a new object into each successively deeper call. This didn't work well for JavaScript and so they changed the code to be iterative, for those not familiar with the differences, I've written a little example of a recursive and an iterative function that performs the same task. Notice that the top function calls itself repeatedly until it has the answer.
document.write('Recurvive:' + recursive(5) + '
'); document.write('Iterative:' + iterative(5) + '
')function recursive(x) { if (x >= 10) return x; return recursive(x + 1); }
function iterative(y) { if (y >=10) return y; while (y < 10) { y++; } return y; }
Using the IE9 Developer tools you can see that the recursive function (on the left) is called 6 times where as the iterative version (on the right) is called just once. The the case of cut the rope the iterative approach performed better.
As JavaScript doesn't have structs or classes it's common to use prototypes as an alternative. The game developers soon realised that taking this approach used more memory as creating an object in JavaScript is much more expensive. Rather than creating copies of objects whenever possible the developers tried to copy individual properties and avoided creating entirely new object instances.
As well as giving some insight into the development process the team also talk about the tools they used to build the application. I've listed them all below, I'm particularly interested in giving the sound manager a spin:
- Simple JavaScript Inheritance a pattern from John Resig
- Visual Web Developer 2010 Express this is a free version of the my tool of preference Visual Studio 2010 Ultimate
- pxLoader - A nice HTML5 image preloader
- Sound Manager - Used to make HTML5 audio easier to deal with and helps get past some bugs in Chrome Audio implementation by falling back to Flash
- IE9 Developer tools (press F12 in IE9) - I gave a talk about the tools in Las Vegas last year if you haven't used them before.