Retro Style Platform Runner Game for mobile with EaselJS (Part 1)

Editors Note: The tutorial is a little bit outdated now, but it still works. However if you would like to learn more on this topic and get a more detailed verison plus all the extras and resources, you should check out my ebook: From Zero to the Appstore - Blueprints for an HTML5 Game.

Here I’m starting a series of 4 articles on how to create a small game with EaselJS that “can” run on your mobile device (unfortunately it won’t run on all mobile devices) – however I also plan on a future posting on how to use technologies like cocoonJS to create a WELL-PERFORMING native app from this.

The FINAL result


The result is going to look similar to this (but with better code!) – In case you take a look at the source-code: This is NOT a good example on how to code cleanly – I did this as a quick mashup to try out a few things. – You can also do another jump, while in the air:

To test this on your mobile device, point your mobile browser to: http://demos.indiegamr.com/jumpy

The result of Part 1&Part 2


Let’s get started with the basics – Setting up the Stage


Working with EaselJS is pretty simple, especially when you worked with ActionScript3 before, as a lot of the methods and classes are based on ActionScript3-equivalents. So basically the first thing you have to do in order to see a result, is to create a canvas-element and set up the “Stage” here is how this can be done:

However, if you tried this out, it wouldn’t bring you that much of a result, because in order to actually see something the Stage has to be ‘updated’ (stage.update();) by EaselJS, also this is something, that has to be done every frame – so to do that, we can setup an event, that will update the Stage every frame.

Great! Now we have a stage, that is updated with 30 frames/second.

More basics – Adding something to the stage


Now that we’ve setup the Stage, we can obviously still see nothing, because there is no object/image there to display.

The basic flow of doing this is to: 1) Preload an Image then 2) Create an DisplayObject with that image and finally 3) add this DisplayObject to the Stage(or another Container)

Preloading:
Some might ask here, why I’m not introducing PreloadJS to preload all the assets (or in this case: one asset) – well PreloadJS is a very nice way to do so – and I also recommend using it for some projects, but I tried running the app with the cocoonJS-launcher and receiveed a big load of Errors from cocoon(due to cocoon’s limited JS-parsing). Now this is NOT the fault of PreloadJS, however since preloading assets is nothing super-complicated, that requires super-extensive knowlege(basically one function), I chose to use an own 5-line implementation to achieve this.

Great! – So how do I make a game from this?


Well, a typical game usually needs the following things:

User Input


To listen to userinput-events we first have to distinguish if the device is a touch-device or has a regular screen and add the according event listeners.

This is pretty straight forward and should widely explain itself.

Movement


To create movement, the simples thing to do is e.g. to alter the position of an object a little bit every frame, just like this:

However in order to get the whole thing not too messy, once there are more objects involved in the scene, we should setup an own class for each object, or at least each object that is moving, as an example I put together the following:

With the Hero-class implemented, we can now create a hero object and apply some interaction, all together it would look like this:

here’s how it should look like(depending on the background-color, that you chose for your html-body):

Of course this is very basic, and the movement is absolutely not realistic, since there is no acceleration in the movement, but we can fix this by adding a velocity-object to the hero, just add the following:

and this is how it should look like by now:

Awesome, now we have a basic “game”, with a character falling down, that will be reset on any user-input. The next parts are going to cover collisions and animations.

You can download the source-files here: download


Agenda

 

20 thoughts on “Retro Style Platform Runner Game for mobile with EaselJS (Part 1)

  1. Also,we are improving the script injection support, we are confident that frameworks like PreloadJS will be supported in CocoonJS in the near future. Right now we have document.createElement(“script”) support but the scripts must be added to the document body using document.body.appendChild.

  2. Pingback: Retro Style Plattform Runner Game for mobile with EaselJS (Part 2) | indiegamr

  3. Pingback: Retro Style Platform Runner Game for mobile with EaselJS (Part 3) – adding movement & more collision | indiegamr

  4. Pingback: Retro Style Platform Runner Game for mobile with EaselJS (Part 4) – adjusting to mobile displays | indiegamr

  5. Pingback: Retro Style Platform Runner Game for mobile with EaselJS (Part 5) – Animations and polishing | indiegamr

  6. Thanks for this helpful tutorial.

    I’m looking now at keyboard input. It looks like you used DOM event handling — does EaselJS not provide help for keyboard events, like it does for mouse events?

    • Actually I don’t know about that – but good point!
      The main-goal for this was to run it also on a mobile device in which case keyboard&mouse are irrelevant ;-)
      But I will look it up and update the article. Thank you.

  7. Pingback: Generate Repeatable Random Numbers (in JS) | indiegamr

  8. For some reason, neither is working for me at the canvas or DisplayObject level.
    I’m using EaselJs 0.6.0 with the latest version of CocoonJS (downloaded today).

    document.addEventListener(‘touchstart’,..) works, but the same call fails when performed on a canvas or a DisplayObject instance.

    I tried also “click” instead of “touchstart”.

    Do you have any suggestion what might cause it?

  9. Well, after setting createjs.Touch.enable(stage), touchstart fires when registered on canvas level, still not on an individual DisplayObject though.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>