Jeff the Unicorn

Sometimes at WeeWorld we like to break away from the constraints of the WeeMee and focus on other original character concepts. Jeff the Unicorn is one such idea that’s been extremely popular with our users, so we thought we’d try and explore some mobile game concepts featuring him. I’ve spent the past four weeks working on an endless runner mobile game demo starring Jeff.

The demo was written in Objective-C and gave me the ideal opportunity to play around with Apple’s Sprite Kit 2D framework. I must say, I really like Sprite Kit. The API is straightforward, the physics integration feels right, and the performance is pretty damn impressive.

We tried to add something different to the endless runner genre by adding a rainbow slide ability. To win the game you must slide on your butt, smearing Jeff’s rainbow coloured poop along the ground for as long as possible. Before Jeff can initiate a slide, you’ll need to collect enough coins and diamonds that are scattered throughout the level.

The demo is really rough and ready but considering we were doing some really rapid prototyping with limited resources I’m really happy how it turned out. Take a look at the video above to see the game in action. As always, sorry about the really shoddy camera work.

Flash and WebGL on Mobile

I got my hands on an HTC One the other day so thought I’d give the demo from my An Introduction to Flash’s WebGL Runtime API tutorial a quick run. When writing the tutorial I’d simply assumed it would work, but I didn’t actually have a WebGL-enabled handset to actually try it on. When my buddy Alex created the original animation and vector artwork, he’d made no attempt to try and optimise it, so I was actually a little apprehensive that the content would just kill the device. Well the good news is that it works a treat and the performance is really quite impressive.

I did have to make a small change to the source code to accommodate touch on mobile, which I’ve now pushed to my GitHub repository. Basically to handle touch on mobile you need to listen for the touchstart and touchend events. Flash’s WebGL export feature holds a lot of promise and it’ll be really interesting to see what people do with it. Take a look at the video above or if you have a WebGL-enabled mobile browser then why not give the demo a spin and let me know how it performs. I’d be interested to know.

Building an HTML5 Flappy Bird clone with Adobe Animate CC: Part 3

Welcome to the third part of this HTML5 Flappy Bird tutorial series. Over the course of the first two parts we created the artwork required for our game. Today we’ll actually start writing the code using the JavaScript programming language.

What you will learn…

  • How to write JavaScript directly within Adobe Animate CC
  • How to write code that utilises content on the stage
  • Some of the fundamentals of the CreateJS suite of JavaScript libraries
  • How to implement endless scrolling within your game world

What you will need…

  • A basic understanding of at least one programming language, such as JavaScript or ActionScript

During the remainder of the series, we’ll write a collection of JavaScript classes that represent our Flappy Bird clone. Each will be responsible for specific tasks within the game. In today’s tutorial we’ll focus on two classes: one which represents the main entry point into the game, and another which deals specifically with the scrolling of the game’s ground layer.

If you need a reminder as to how the scrolling behaves in Flappy Bird then take a look at the final version of our clone. It can be found here: www.yeahbutisitflash.com/projects/flappy-animatecc/flappy.html.

Getting Started

You’ll need Adobe Animate CC. A trial version can be downloaded from www.adobe.com/products/animate.html.

We’ll use the flappy.fla file that you worked on in part two. If you haven’t attempted the first two parts of the tutorial and would rather skip the design stage and jump straight into development, then simply head over to GitHub and get the latest version of the FLA from: github.com/ccaleb/flappy-bird-animate-cc-tutorial/blob/master/start/flappy-artwork.fla.

Okay, let’s get started!

Going Responsive from the Publish Settings

In part two we previewed our finished artwork in the browser using Animate CC’s default publish settings. For many situations these settings will be fine but for this tutorial we’d like our Flappy Bird clone to scale-up to fit the browser window and also resize in response to changes in the window’s width and height. All of this can be done from the Publish Settings panel.

Open the Publish Settings panel by selecting File | Publish Settings from Animate CC’s dropdown menu. From the panel, ensure that the JavaScript/HTML checkbox is checked and that the Basic tab is selected.

figure-1b

Figure 1. Responsive Publish Settings.

Firstly, we’d like our game to vertically and horizontally centre align with the browser window, so check the Centre Stage checkbox and also select Both from the dropdown box next to it.

Next, click the Make responsive checkbox. Since we want our game to resize whenever the browser’s width or height is changed, select Both from the dropdown box associated with the checkbox.

Also check the Scale to fill visible area checkbox. Now we need to decide how scaling of our viewport will be handled within the browser. We have two options. We can ensure that our game’s entire viewport is always visible, which may result in visible border spaces surrounding it. Or we can scale the viewport to cover the entire screen but at the risk of some of the viewport being cropped. For our game it’s best if the entire viewport is always visible so select Fit in view from the dropdown box to the right of the Scale to fill visible area checkbox.

Use Figure 1 above to verify that you have the correct settings then click OK to close the Publish Settings panel.

Now let’s test our changes in the browser to see exactly what our publish settings are doing for us. Remember, at the end of part two we had to temporarily guide out the Screen Flash layer before testing. Do the same again here then select Control | Test Movie | In Browser (Cmd-Enter | Ctrl-Enter) from Adobe Animate CC’s dropdown menu.

You’ll see that your game’s viewport now consumes all of the browser’s vertical screen space. Adjust the width and height of the browser window. The game’s viewport will adjust to fit the new window size and also maintain its aspect ratio. Congratulations! Your content is now fully responsive.

Move back to Animate CC and un-guide your Screen Flash layer again, then save your FLA.

Writing JavaScript within Adobe Animate CC

While it’s perfectly possible to write code externally using an editor of your choosing, we’ll write all our code within Animate CC. We’ll do this by adding scripts to our FLA’s timeline.

Begin by creating a layer folder on your root timeline named JavaScript. Add a layer within your folder and name it Main class.

figure-2b

Figure 2. The timeline, stage and Actions panel.

We’ll add some JavaScript to this layer, which will be the main entry point for our game. This is done by clicking on the layer’s frame and selecting Window | Actions (F9) from Animate CC’s dropdown menu. Doing so will open the Actions panel where we’ll place our code (Figure 2).

Creating the Main Entry Point and Update Loop

We’ll write a class named Main. Within its constructor we’ll listen for a special tick event that gets dispatched every time the stage is redrawn. To do this, add the following code to the Actions panel:

function Main()
{
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Now add an update() method to the class that handles the event:

function Main()
{
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Main.prototype.update = function(evt)
{
  console.log("Main::update()");
}

The method doesn’t do much at the moment. It simply writes some text to the browser’s console. If you remember from the first part of this tutorial, we set our document’s frame rate to 60 fps. Therefore our update() method will get called, and will write text to the console, 60 times per second.

As you can see, we’re adopting an object-oriented approach for the development of our Flappy Bird clone. JavaScript 5th edition doesn’t provide the syntactic sugar found in other languages but its prototype mechanism can be used to simulate many class-based features.

We’ll aim for a very lightweight object-oriented approach in this tutorial series. We’ll use JavaScript functions to simulate class constructors and declare properties within those functions that act as member variables. Class methods will be applied via a constructor’s prototype property. All methods and member variables within our class’ will be publicly accessible.

If you aren’t familiar with Object-Oriented programming techniques then don’t worry. We’ll keep things straightforward enough that you’ll still be able to follow along.

Think of our update() method as the game’s heartbeat. All the game’s logic will eventually be run from here and will be continuously executed during the game’s lifetime within the browser.

Finally, we’ll instantiate the Main class to ensure its constructor runs. Add the following line:

function Main()
{
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Main.prototype.update = function(evt)
{
  console.log("Main::update()");
}

var main = new Main();

Save your FLA file.

Testing your Code

Let’s go ahead and test the code we have so far. Republish your FLA by selecting File | Publish (Alt + Shift + F12 | Ctrl + Shift + F12) from Animate CC’s dropdown menu. Now refresh your browser to load and run the latest version of your game.

Since we un-guided the Screen Flash layer a few moments ago, you’ll be greeted by a white screen. However, your Main class’ update() method will now be getting called on each frame update. You can verify this by checking your browser’s console. To do this from Chrome, select Tools | Developer Tools (Cmd + Option + i | F12) to open the Developer Tools window. Then click the Console tab to open the JavaScript console window. Here you should see the text from your update() method being traced out (Figure 3).

figure-3b

Figure 3. The console window.

The console is also a great way to interactively call code or view any errors in your JavaScript. If any errors are being reported then correct your code, republish, and test.

Accessing Display Objects

Okay, let’s do something about the white screen flash movie-clip that is covering the stage. Of course, we can simply guide it out from the timeline but let’s actually write some JavaScript to deal with it.

Move back to Adobe Animate CC and add the following line of code within your constructor:
function Main()
{	
  exportRoot.screenFlash.visible = false;
  
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

The above line, simply gets a reference to our screen flash movie-clip and hides it from view.

exportRoot is a special global variable that is made available to you. It represents your stage and all the content within it. If you think back to part two, you should remember that we assigned an instance name of screenFlash to the movie-clip that represents the screen flash. We can access that movie-clip via the exportRoot object. In fact, we can do this for any display object within the stage that has an instance name assigned to it.

Once we have referenced the screen flash, we can then work with the various properties and methods associated with it. In this case, we want to set its visible property to false in order to hide it.

Adobe Animate CC is integrated with CreateJS, which is a suite of JavaScript libraries that enables rich interactive content via HTML5. Many of CreateJS’s APIs are loosely modelled on Adobe’s own ActionScript 3 API and will therefore feel familiar to anyone who has created Flash content using ActionScript.

For example, the EaselJS library – which is a part of the CreateJS suite – provides a MovieClip class. We’re just after using the MovieClip class to set the visibility of our Screen Flash movie-clip. It provides many properties and methods that are similar to the ActionScript 3 equivalent. You can find the CreateJS API reference for MovieClip along with other classes which we’ll use throughout this tutorial here: www.createjs.com/docs/easeljs/modules/EaselJS.html.

You can also find code examples and reference material for the entire CreateJS suite on the site’s official homepage: www.createjs.com.

While we’re at it, let’s go ahead and temporarily hide most of the other UI elements – we’ll make them visible again at a later date but right now they’re simply acting as a distraction. Add the following lines, which directly access the gameOverPrompt, getReadyPrompt, and startInstructions movie-clip instances that are sitting on the stage:

function Main()
{	
  exportRoot.screenFlash.visible = false;
  exportRoot.gameOverPrompt.visible = false;
  exportRoot.getReadyPrompt.visible = false;
  exportRoot.startInstructions.visible = false;
  
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Save your changes and republish. Refresh the browser and you should now see the contents of your stage. The screen flash should be hidden along with various other UI elements.

Detecting Mouse and Keyboard Presses

In order to make our game’s little hero flap his wings, the user will need to either click on the screen or press any key on their keyboard. Let’s go ahead and write some JavaScript to check for user interaction.

Within the constructor, add a few lines of code that will call a userPressed() method in response to the user clicking on the canvas or pressing a key:

function Main()
{	
  exportRoot.screenFlash.visible = false;
  exportRoot.gameOverPrompt.visible = false;
  exportRoot.getReadyPrompt.visible = false;
  exportRoot.startInstructions.visible = false;

  canvas.onmousedown = this.userPressed.bind(this);
  window.onkeydown = this.userPressed.bind(this);
	
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Also, write the actual userPressed() method. Place it directly below the update() method:

Main.prototype.update = function(evt)
{
  console.log("Main::update()");
}

Main.prototype.userPressed = function(evt)
{
  console.log("Main::userPressed()");
}

For the time being it’s a stub method that simply traces some text to the JavaScript console.

Save and republish your FLA. Refresh your browser and test the latest changes by clicking on the game’s viewport or by pressing some keys on your keyboard. Each of your interactions should result in the following text being sent to the JavaScript console window:

> Main::userPressed()

As well as making the bird flap its wings, the userPressed() method will be responsible for starting a new game. When a game begins, the ground layer and pipes will start scrolling. When the game is over, the ground and pipes will stop scrolling.

Pinning Scripts

We’ll concentrate on the ground layer for the remainder of this tutorial. We’ll create a class named Ground to handle the logic for it. Just like our Main class, this class will have a constructor used to initialise it and an update() method that acts as its heartbeat.

Before we begin, let’s pin our Main class to the Actions panel. Pinning allows us to quickly move between each of our classes within the Actions panel as we work.

figure-4b

Figure 4. Pinning your script to the Actions panel.

Ensure the Actions panel is visible. If it’s not then find the Main class layer on the timeline. If you look at the first frame you’ll see that it has an icon indicating that your code is attached to it (Figure 4). Click on the frame then open the Actions panel by selecting Windows | Actions (F9) from Animate CC’s dropdown menu.

Within the Actions panel you’ll see the JavaScript for your class and above your code you’ll see the Pin Script button (Figure 4). Click the button and a new tab will be created (Figure 4) containing your Main class’ code. This tab will remain within the Actions panel even after you close the panel.

We’ll do the same for our Ground class when we start writing it.

The Ground Class

Within Animate CC, create a new layer directly above the Main class layer. Name the layer Ground class and click on its first timeline frame which is where we’ll add the code. If the Actions panel isn’t visible then select Window | Actions (F9) from the dropdown menu.

Within the Actions panel, click on the Current frame tab, which is the tab to the left of your Main class tab. You’ll be presented with an empty script window. Pin this window by clicking the Pin Script button. A new tab will be created named Ground class, which is where we’ll write our code.

Begin by writing the class’ constructor. Declare a member variable within the constructor that will be used to track whether the ground is scrolling:

function Ground()
{
  this.scrolling = false;
}

Add a simple update() method that, for the time being, traces some text to the console when scrolling is taking place:

function Ground()
{
  this.scrolling = false;
}

Ground.prototype.update = function()
{
  if (this.scrolling == true)
  {
    console.log("Ground::update() scrolling");
  }
}

Adding Start and Stop Methods

We’ll also need methods to initiate scrolling of the ground layer and to stop it. Add a startScrolling() and a stopScrolling() method to your class:

Ground.prototype.update = function()
{
  if (this.scrolling == true)
  {
    console.log("Ground::update() scrolling");
  }
}

Ground.prototype.startScrolling = function()
{
  this.scrolling = true;
}

Ground.prototype.stopScrolling = function()
{
  this.scrolling = false;
}

At the moment our Ground class won’t do much other than write some text to the console whenever scrolling is taking place. Shortly we’ll start adding code to actually deal with the movie-clips within our stage that represent the ground layer. But first let’s wire-up the Ground class to our Main class.

Adding the Ground Class to the Update Loop

Our Ground class’ update() method currently isn’t being called anywhere. We’ll fix that by calling it from within the Main class’ own update() method.

Within the Actions panel, click on the Main class tab to view its code.

Instantiate the Ground class and store it within a member variable:

function Main()
{
  this.ground = new Ground();

  exportRoot.screenFlash.visible = false;
  exportRoot.gameOverPrompt.visible = false;
  exportRoot.getReadyPrompt.visible = false;
  exportRoot.startInstructions.visible = false;

  canvas.onmousedown = this.userPressed.bind(this);
  window.onkeydown = this.userPressed.bind(this);

  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Within the update() method, call the Ground instance’s own update() method. While you’re at it, remove the previous log() statement:

Main.prototype.update = function(evt)
{
  console.log("Main::update()");
  this.ground.update();
}

Now all that’s left to do is to trigger the scrolling of the ground when the user interacts with the game. Add a new method named startGame() and call it from your userPressed() method. Also remove the log() statement that’s within your userPressed() method.

Main.prototype.userPressed = function(evt)
{
  console.log("Main::userPressed()");
  this.startGame();
}

Main.prototype.startGame = function()
{
  this.ground.startScrolling();
}

Save your FLA.

Now republish and test your changes within the browser.

The Ground class’ update() method is now being called on every frame update along with the Main class’ update() method. However you won’t see anything being traced to the JavaScript console because the ground’s update() method only writes to the console when scrolling has been initiated.

Go ahead and click anywhere on the screen to initiate scrolling. The ground’s startScrolling() method will get called, which will result in the following text being repeatedly output to the console:

> Ground::update() scrolling

If you can see this then your code is working as expected.

Working with the Ground Slice Movie-Clips

Okay, now that we have the Ground class wired up to our Main class, we can go ahead and start working with the actual movie-clip instances that represent the ground.

If you unlock the timeline’s Ground layer and take a look at the stage, you’ll see that we have three movie-clips named ground0, ground1, and ground2. Each of these clips represent a vertical ground slice and are sitting adjacent to one another to create the ground layer.

Remember, we can take advantage of the global exportRoot variable to access any display objects on our stage. Let’s write some code within our Ground class’ constructor to store all three ground slices within an array.

Within the Actions panel, move back to the Ground class tab and add the following line to the class’ constructor:

function Ground()
{
  this.scrolling = false;
  this.slices = [exportRoot.ground0, exportRoot.ground1, exportRoot.ground2];
}

We’ve stored each of our ground slices within an array named slices. We have declared this array as a member variable, which allows us to easily access the ground slices within any method in our class. The order of the array’s elements is important. Each slice is stored in the array based on its position relative to the screen’s left-hand side. The slice at the front of the array is the closest to the screen’s left while the slice at the end of the array is the farthest away. This ordering will come into play soon.

We want to move each of our ground slices every time the Ground class’ update() method is called. We’ll write a method named updateSlicePositions() to do that, but first add a line of code to actually call the method:

Ground.prototype.update = function()
{
  if (this.scrolling == true)
  {
    console.log("Ground::update() scrolling");
    this.updateSlicePositions();
  }
}

Now add the method itself:

Ground.prototype.stopScrolling = function()
{
  this.scrolling = false;
}

Ground.prototype.updateSlicePositions = function()
{
  for (var i = 0; i < this.slices.length; i++)
  {
    var slice = this.slices[i];
    slice.x -= Main.SCROLL_SPEED;
  }
}

The updateSlicePositions() method is fairly straightforward. A for-loop is used to walk through our slices array and pick out each of the ground slice movie-clips. The x-position of each movie-clip is then moved to the left slightly. A constant named SCROLL_SPEED is used to specify the number of pixels to move each of the slices by. That constant belongs to the Main class and will also be used to move the game’s pipes when we eventually get onto them. It hasn’t yet been added so let’s do that now.

Within the Actions panel, move to the Main class and add the following line directly below the class’ constructor:

function Main()
{
  this.ground = new Ground();
	
  exportRoot.screenFlash.visible = false;
  exportRoot.gameOverPrompt.visible = false;
  exportRoot.getReadyPrompt.visible = false;
  exportRoot.startInstructions.visible = false;
	
  canvas.onmousedown = this.userPressed.bind(this);
  window.onkeydown = this.userPressed.bind(this);
	
  createjs.Ticker.addEventListener("tick", this.update.bind(this));
}

Main.SCROLL_SPEED = 3.0;

We’ve assigned a value of 3.0 to the SCROLL_SPEED constant meaning that each of the ground slices will be moved to the left by 3 pixels every time the stage is redrawn.

Let’s test what we have so far. Save your changes and republish. Now refresh your browser and click anywhere on the screen. The ground layer will start scrolling and eventually disappear out of the left-hand side of the screen.

This is a definite step in the right direction but to create endless scrolling we need our ground slices to wrap back around and re-appear from the screen’s right-hand side. Let’s go ahead and write some code to do that.

Repositioning the Ground Slices

Move back to Animate CC.

We’ll need to know when a ground slice has fully left the screen. We can determine this by checking to see if a slice has moved beyond a certain x-position. To work out what that x-position is we’ll first need to discover the width of our ground slices. We can determine the width by simply measuring the distance between the position of the first and second ground slice.

Move back to the Ground class and add the following line to the Ground class’ constructor:

function Ground()
{
  this.scrolling = false;
  this.slices = [exportRoot.ground0, exportRoot.ground1, exportRoot.ground2];
  this.sliceWidth = exportRoot.ground1.x - exportRoot.ground0.x;
}

As you can see, we’ve stored the width in a member variable named sliceWidth. This will allow us to easily access the width throughout the class.

Now that we know the width we can easily work out the x-position each slice needs to reach before it is off-screen. We know from looking at our stage that when the game begins the first ground slice sits perfectly against the left-hand side of the screen. So calculating the off-screen x-position is simply a matter of subtracting the ground slice width from the first slice’s x-position:

function Ground()
{
  this.scrolling = false;
  this.slices = [exportRoot.ground0, exportRoot.ground1, exportRoot.ground2];
  this.sliceWidth = exportRoot.ground1.x - exportRoot.ground0.x;
  this.leftBound = exportRoot.ground0.x - this.sliceWidth;
}

As you can see from the above code, we’ve stored that value within a member variable named leftBound so that we can easily access it elsewhere within the class.

We’re about to write a method named checkLeftSliceIsOutsideScreen() that will check to see if the left-most ground slice has moved outside the screen. If it has then this method will take that slice and position it directly after the right-most ground slice, which will result in it being just outside the screen’s right-hand side. First though, let’s add a line to our update() method to call the checkLeftSliceIsOutsideScreen() method:

Ground.prototype.update = function()
{
  if (this.scrolling == true)
  {
    this.updateSlicePositions();
    this.checkLeftSliceIsOutsideScreen();
  }
}

Okay, now let’s write the checkLeftSliceIsOutsideScreen() method. Add the following at the end of your class:

Ground.prototype.checkLeftSliceIsOutsideScreen = function()
{
  var firstSlice = this.slices[0];
  var lastSlice = this.slices[2];
  if (firstSlice.x < this.leftBound)
  {
    firstSlice.x = lastSlice.x + this.sliceWidth;
    this.slices.shift();
    this.slices.push(firstSlice);
  }
}

Let’s examine our method in a bit more detail to better understand how it works.

The first two lines simply get references to the left-most and right-most ground slices and store them within local variables. Remember, the items within the slices array are ordered so we know that the left-most ground slice is always at the front of the array while the right-most ground slice is always at the end of the array:

var firstSlice = this.slices[0];
var lastSlice = this.slices[2];

We then check to see if the left-most slice has moved outside the left-hand side of the screen:

if (firstSlice.x < this.leftBound)

If it has, we position the ground slice directly after the right-most slice:

firstSlice.x = lastSlice.x + this.sliceWidth;

Now that the slice is on the far right of the screen, we need to remove it from the front of the slices array and place it at the end:

this.slices.shift();
this.slices.push(firstSlice);

Let’s try out our latest changes. Save your FLA then re-publish. Refresh your browser and click anywhere on the game’s viewport. You’ll see the ground layer scrolling indefinitely. Each slice will re-appear from the right-hand side of the screen after it has scrolled out of the left.

Congratulations! We have an endless scrolling ground layer.

Summary

We’ve covered significant ground in part three. Not only can Adobe Animate CC be used as a design tool, it can just as easily be used to write the JavaScript for your HTML5 projects.

We’ve walked through the steps to write a main application class for our game and a class to scroll the game's ground layer. Along the way we’ve seen how to write code that works directly with the contents of our stage, hooking into some of the visual elements created in parts one & two. Additionally we’ve written JavaScript that responds to the user’s mouse and keyboard interactions.

Now that scrolling for the ground layer is up and running it won't be that difficult to get the game's pipes scrolling too. However, I'll let you digest what's been covered today and we'll tackle the pipes in part four. See you then.

Christopher Caleb

ccaleb
Christopher Caleb is a freelance developer and published author. His body of work spans a wide range of projects that encompass casual games, interactive kiosks, social networks, and mobile applications. Christopher's expertise covers both native and cross-platform technologies, with a focus on optimisation within hardware constraints. He specialises in delivering highly engaging experiences with usability being a primary concern.

He blogs at www.yeahbutisitflash.com and tweets as @chriscaleb.

An Introduction to Flash’s WebGL Runtime API

Learn how to build interactive WebGL content using Adobe Flash Professional CC 2014 that will run across both desktop and mobile browsers.

What you will learn…

  • How to publish for WebGL from Flash Professional CC 2014
  • The basics of Flash’s WebGL Runtime API
  • How to work with Flash movie-clips using JavaScript

What you should know…

  • You should be comfortable working with Adobe Flash Professional
  • A familiarity with ActionScript or JavaScript is required

With the release of Adobe Flash Professional CC 2014 you can target WebGL-enabled browsers, allowing your content to run across both desktop and mobile devices, while taking full advantage of GPU acceleration. If you’re familiar with Flash’s existing HTML5 Canvas export option you may be asking yourself what the distinction between that and the WebGL target is? While both draw to an HTML5 canvas element, WebGL utilises the GPU for superior rendering performance. Additionally, while the two export options provide a JavaScript API, both APIs currently differ.

This tutorial will take you through the designer-developer workflow. You’ll learn how to export your FLA’s graphical resources to WebGL, before adding interactivity to it using JavaScript and Flash’s WebGL runtime API. We’ll be creating a prototype beat ’em up game where your hero can perform either an attacking or blocking move. By doing so you’ll learn how to work with movie-clips and how to handle and respond to simple mouse (or touch) interactions. For a clear idea of what you’ll be building, take a look at the final result below.

For the avoidance of doubt. What you are looking at here is Flash-authored content running in your web browser but outside of Flash Player. By publishing to WebGL you are potentially increasing the reach of your Flash content to devices that don’t support Flash.

Before we proceed, a special mention must go to my buddy Alex Fleisig, who has provided the artwork and animation for this tutorial. Alex has worked for the likes of Pixar and DreamWorks on a wealth of hit movies. You can see some more of his animation work here.

Continue Reading

Adobe AIR Games Showcase #4

It’s time to take a look at some more great games built with Adobe AIR. If you’re using the Flash platform, or any other development platform for that matter, to build your own games then hopefully you’ll find some inspiration here. If you’re just looking for a few games to play then I don’t think you’ll be disappointed either.

Haunt the House: Terrortown

Powered by Adobe AIR and the Starling framework, Haunt the House: Terrortown is another stunning example of what can be created with the Flash platform. It can best be thought of as a side-scrolling action puzzle game starring a ghost, where the objective is to possess objects in order to scare the hell out of people. In fact, if you’re really good at it you can scare some of your poor victims to death!

The game takes place over five vibrant locations, each with its own musical style and unique objects. In fact, each location has hundreds of different objects for you to possess, with several funny outcomes from moving and interacting with each. Get the timing right and you’ll have town folk running for cover, or darting towards the nearest exit. Haunt the House has some splendid animation and a great sense of humour.

It’s available on Steam for both Windows and Mac.

Infectonator: Hot Chase

Zombie are great. Endless runners are great too. So I’m delighted that Armor Games had the good sense to combine both to make Infectonator: an awesome zombie game where you rampage through a city infecting as many unfortunate victims as possible.

Unlike the traditional slow shambling zombies we’re used to, Infectonator’s zombie protagonist runs after his prey. Unfortunately he burns out quickly and needs fresh victims to replenish his stamina and boost his speed. Fail to infect enough people and you’ll grind to a halt. With coins to collect, power-ups to earn, and high-scores to bag, there’s more than enough to keep you coming back for some time.

Infectonator: Hot Chase is available for iOS on the App Store and Android via Google Play. And of course, being built using the Flash platform, you can also play it on the web too!

Mucho Party

I really like Mucho Party. It doesn’t take itself too seriously, offering up 30 fun mini-games for you to compete against friends or AI players. Each game is wacky, simple to play, and has a sense of humour that will keep players smiling as they compete and attempt to unlock new games.

While each game is straightforward and only takes seconds to play, Mucho Party will adjust its difficulty based on your own skills. So players of a wide range of ages and abilities can take part. Basically it makes itself as accessible to as wide a range of people as possible, which is exactly what an app of this type should. Great stuff!

Mucho Party is available on the App Store and Google Play.

CSI: Hidden Crimes

It’s great to see large games companies such as Ubisoft using Adobe AIR. It’s even better seeing them use it to build games based on big name TV shows such as CSI. At its heart, CSI: Hidden Crimes is a simple find-the-object game. You use your observation skills to hunt for evidence at crime scenes. Analysing the collected evidence leads you to new crime scenes and suspects. As you continue investigating leads you’ll start to build up a profile of the murderer, allowing you to ultimately solve the case.

CSI: Hidden Crimes is an extremely slick game that has a great visual style that captures the mood of the TV series without being too gory given the game’s subject matter. It’s easy to get in to and doesn’t demand too much of your time in any one sitting, which makes it ideal for casual gamers. CSI: Hidden Crimes is available from the App Store for those with iOS devices and Google Play for Android owners.

NANACA†CRASH!!

Note: Just found out that NANACA†CRASH!! isn’t actually an AIR app. Check out developer Kick Lensing’s comment at the bottom of the post.

It started life as a crazy Japanese anime web game and now NANACA†CRASH!! has made it onto mobile. It’s hard to actually put into words just how bonkers NANACA†CRASH!! actually is, but I’ll try. Basically the game begins with your character being sent flying into the air after being hit by a bike. You then spend your remaining time trying to see just how far you can keep him flying through the air.

It seems simple, but NANACA†CRASH!! has a surprising amount of depth and before long you’ll be uncovering tricks and moves that will keep your character soaring through the air. On my first attempt I only managed a distance of 50 metres. However, before long I was covering distances of several kilometres at a time. Cool!

NANACA†CRASH!! is available on the App Store and Google Play.

Swift Endless Runner Source Code

As you may have already heard by now, Apple has announced a new programming language called Swift. It’s modern, powerful, and potentially easier to use than Objective-C. In fact, from the time I’ve spent tinkering with it, I’d say that those coming from an ActionScript or JavaScript background should find Swift a much more approachable language. So if you’ve always wanted to develop iOS and Mac OS X applications then this might be the ideal time to jump in.

I had a bit of spare time over the last few days so thought I’d flex my programming muscles and port the parallax scroller from my endless runner demo to Swift. Truth be told it was a fairly straight forward process and I’m looking forward to spending more time with the language over the coming months. If you fancy taking a look you can find the source code on GitHub: https://github.com/ccaleb/endless-runner

It’s also great to see a few familiar faces from the Flash community picking up Swift and blogging again for the first time in a while. Adobe’s Thibault Imbert has blown the cobwebs off his ByteArray blog and has already posted several great Swift tutorials for those coming from an ActionScript background. Lee Brimelow, who also works at Adobe, has set up a new blog dedicated to Swift tutorials, which is definitely worth checking out.

If you’re interested in learning Swift then Apple’s Introducing Swift website is a good place to start.

iOS X-wing Targeting Computer

There are new Star Wars films on the horizon and I’m pretty damn excited by that. In fact I’m so excited I decided to re-write my X-Wing Targeting Computer app for iOS. Yay!

You may remember the original ran on Android handsets and was written using Adobe AIR. This time I thought I’d go native and write it in Objective-C. I also thought the app needed a lick of paint too so enlisted the help of my sister-in-law who very generously offered to spend her very limited free time making it all look a bit more modern. I’m sure you’ll agree it looks awesome!

iPhone-5-Screenshot-1

You may be thinking why I bothered with Objective-C. After all, since the Android version was written in AIR, couldn’t it easily be ported to iOS too? Yup, it definitely could, but I don’t do nearly enough Objective-C stuff and thought the app gave me the ideal excuse to do some more.

iPhone-5-Screenshot-3

If you aren’t familiar with the original version, it’s a simple GPS-enabled app that lets you pretend to be an X-wing pilot while you’re driving your car. It’s really easy to use. Just lock in your target location and start driving until you eventually reach the exhaust port, er, I mean destination. Your distance to the target will be continuously updated as you drive and you can even hear radio chatter from your fellow pilots!

iPhone-5-Screenshot-2

Just like the original version, I have no plans to release it on the App Store. I strongly suspect I’d have the same problem convincing Disney to release the app as I did with Lucas Licensing previously, so I’ll leave it as a nice portfolio piece.

iPhone-5-Screenshot-4

I’ve added a video at the beginning of this post where you can see me giving it a quick spin. I couldn’t be bothered actually driving around in my car with it (you can see a video of me doing that with the original Android version here) but you can see it run in demo mode to give you an idea of how it works.

Now that that’s all done and dusted it’s time to start learning how to program iOS apps in Swift. Anyway, may the Force be with you guys. Always.

Integrated Native Physics in the Flash Runtime

Over the last year or so there’s been much talk from the community regarding the need for the integration of native physics within the Flash runtime. Adobe’s Flash product manager, Chris Campbell, has opened this up for further discussion on the Adobe Communities forum.

Personally I think this might be a good thing as Adobe has been saying for some time that gaming is now the primary focus of Flash and AIR. However, I really only want to see this happen if adding a native physics engine would provide superior performance over and above using a third party ActionScript library. I’d also only really be keen to see this happen if Adobe were committed to frequently maintaining and updating the API. Finally, I’d prefer that API to be either Box2D or something that’s very similar to Box2D. My reason being that Box2D is a very popular and well document API. I’d rather work with an API that’s well known and already has a wealth of tutorials and resources out there that I can already benefit from.

Anyway, if you want your say in the matter then head over to the Adobe Community Forums and add your tuppence worth.

Unity WebGL Export

Isn’t Unity awesome! For 3D games developers it’s probably one of the best choices out there and seems to run on just about any hardware platform you can throw at it. Now with Unity 5, things are getting even more awesome as it now targets WebGL, removing the need for the Unity browser plugin.

So how does all this work? Well basically the Unity runtime code needed to be cross-compiled to JavaScript allowing it to run in your browser. This was done using emscripten to convert the runtime’s C and C++ codebase into asm.js JavaScript. I’ve mentioned asm.js a couple of times before. It’s an optimised subset of JavaScript that can be AOT-compiled by supporting browsers into native code. The beauty of asm.js is that being a subset of JavaScript, it will still run on browsers that don’t directly support it ensuring a level of cross-browser compatibility.

Of course, Unity game code itself is written in C# (and UnityScript) and compiled into .NET bytecode. That bytecode also needs to be converted to JavaScript before your game will run in a WebGL enabled browser. To achieve that, the Unity guys developed a new technology called IL2CPP, which takes your game’s bytecode and converts it all to C++. Emscripten is then once again employed to convert all that to JavaScript.

Phew! Sounds pretty technical but it results in your Unity games being able to target WebGL compatible browsers and that’s a very exciting prospect for many. Theoretically your games will run on browsers and platforms that don’t have support for the Unity browser plugin. For example, this could open up Unity games to Android phone and tablet owners wishing to play games within their browser rather than downloading apps. And if Apple ever bothers to support WebGL on mobile Safari then we could also see Unity games running in the browser on iOS.

It’s early days yet and Unity’s WebGL support is still in Early-Access. However I was able to run and test a few demo games on my Macbook using Safari. I must say, I was very impressed. If you fancy taking a look then try out both Dead Trigger 2 and AngryBots, which show off the technology’s potential. Great stuff!

Now I wonder if Adobe will go down the same route with Flash and break away from its dependency on the browser plugin. It could breathe new life into the much-maligned runtime and give Flash a wider reach on mobile phones and tablets.

Adobe AIR Games Showcase #3

I’ve come across quite a number of Adobe AIR games over the last few months so thought it was about time I did another round-up. There’s actually too many AIR games out there these days, particularly on mobile, so unfortunately I can’t cover them all. However here’s five that caught my eye for one reason or another. There are a few more I’ll cover in a future post but if you’re an ActionScript 3 programmer and you use frameworks like Starling and Away3D then the following games should let you see what others in the community are capable of.

Groove Racer

I downloaded Groove Racer almost a year ago and didn’t realise it was written in Adobe AIR! It’s a gorgeous little racing game that recreates the thrills and spills of Scalextric tracks right on your iPhone. Controls are simply a case of holding your finger on the screen to force your car to accelerate. If you want to beat your opponent however then you’ll need to carefully judge your speed coming into the bends. With 10 unique cars across 66 imaginative tracks there’s plenty to keep you entertained for some time. Download Groove Racer for iPhone, iPod touch, and iPad from the App Store.

The Banner Saga

The Banner Saga is a truly epic tactical role-playing game inspired by Viking legend. It’s also a game of choice, where a poorly judged decision can see your caravan suffer or even seal the fate of individuals you’ve grown fond of. When you’re not spending time managing, you’ll be engaged in the game’s grid-based combat desperately trying to win victory in order to push on for another day. It’s a stirring game that gnaws at your emotions, taking you on an epic journey through beautifully crafted hand-painted landscapes. The Banner Saga is available for Windows and Mac. Also, if you want to know more then take a read of both Eurogamer’s and IGN’s reviews, which do the game much more justice than my brief description ever can.

Hilomi

Here’s another beautiful 2D platform/puzzle game written in AIR. Guide Hilomi, the game’s heroine, through each level taking photographs of the unusual wildlife that inhabits her world. Hilomi needs help traversing each level’s environment and that’s where you come in. You can deform the terrain with your fingertips: moving earth, creating bridges, digging tunnels, and commanding water and fire, all in a bid to get Hilomi to each of the animals she wants to add to her photo collection. While the initial dozen or so levels are pretty easy, the game’s difficulty soon ramps up making it a great distraction for anyone who likes puzzle games, like my girlfriend who’s sitting next to me desperately trying to figure out how to get onto the next level. Hilomi is available for iOS from iTunes and Android via Google Play.

Zombie 300

Zombies have invaded your mobile screen! Use your fingers to crush them and save the humans. It’s more or less that simple in this delightful little game for iOS and Android. Of course other things have been added to the mix to provide more depth including: power-ups, a collectible currency system, and innocent bystanders. During the heat of the action it’s quite easy to confuse bystanders with the zombies, which quite often results in humans getting accidentally crushed into the ground. Given the fact that all the humans in the game are represented by schoolchildren it’s all the more heartbreaking when you crush one. My little nephew loves this game, and the distraught expression on his face when he accidentally crushes a boy or girl is priceless. Zombie 300 is available on iTunes, Google Play, and the Amazon App Store.

CUBD

I think CUBD can best be described as a brain-bending 3D puzzle game that’s inspired heavily by the Rubik’s Cube and match-three games. With a simple flick of your finger you can turn and twist your cube in an attempt to match three squares of the same colour. You’ll need to be quick though as you’re up against the clock, and the only way to gain more time is by finding more and more matching colours. It’s both challenging and addictive and things start to really heat up when you’re running low on time. If you’re a fan of puzzle games then you’ll certainly find yourself coming back to CUBD time and time again. It’s available for iOS on the App Store and Android through Google Play.