Jan 27, 2012
Posted on Jan 27, 2012 in Hints and Tips | 10 comments
If you, like many Flash developers, are looking into using HTML5 for your web apps, you’ll almost certainly have come across jQuery. It’s a very popular JavaScript library, used by a large percentage of the most visited websites – but what’s all the fuss about, and should you use it?
Background
If you know AS3, you basically know a lot of JavaScript already; the two languages are very similar. So it’s tempting to just jump straight in and code – but there are a few important concepts you need to understand first. One of these is the idea of the DOM.
When you load a webpage, your browser turns flat HTML into a tree-like structure of JavaScript objects called the DOM (Document Object Model). The JavaScript DOM, then, is very similar to the ActionScript Display List; and if you’re a Flex developer, you’ll be able to see the similarities between MXML and HTML.
In Flash, we can access specific display objects by navigating to them through the display list, like stage.getChildAt(3).getChildAt(0), but this is pretty ridiculous. Instead, we’re more likely to give display objects instance names (through the Flash IDE), or store references to them in variables, arrays, or object properties, like dialogBox.okButton = new SimpleButton().
In JavaScript, we could construct our DOM entirely through JS and then tell the browser to render it, but this is an unusual approach; we’re much more likely to define the DOM via HTML, perhaps with a little JS augmentation. So, JavaScript has various different methods for accessing elements of the DOM.
The simplest of these is document.getElementById(). If we have an HTML document defined like this:
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<div>
<span id="example">
<p>Here's some example text.</p>
</span>
</div>
</body>
</html>
…then document.getElementById("example") will get us the highlighted span object from the DOM. We could then add a second p tag like so:
var newPara = document.createElement("p");
var newTextNode = document.createTextNode("More example text that we created on the fly.");
newPara.appendChild(newTextNode);
document.getElementById("example").appendChild(newPara);
This would update the DOM, making it equivalent to what would have been created if the original HTML was as follows:
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<div>
<span id="example">
<p>Here's some example text.</p>
<p>More example text that we created on the fly.</p>
</span>
</div>
</body>
</html>
What if you wanted to then access the two p elements? We can’t access them directly with document.getElementById(), since they have no ID, but we can use document.getElementsByTagName("p") to obtain an array containing both of them.
And if we’d had another span, like this:
<div>
<span id="example1">
<p>Here's some example text.</p>
<p>More example text that we created on the fly.</p>
</span>
<span id="example2">
<p>A second span.</p>
<p>We don't care about these paragraphs.</p>
</span>
</div>
…and we only wanted to obtain the first two p tags, we could call document.getElementById("example1").getElementsByTagName("p") just to retrieve those two – all these DOM functions work at any level in the tree structure, just like how every Flash DisplayObjectContainer has methods like getChildAt().
This is simple enough to understand, but there are problems. The first, you may not be surprised to hear, concerns Internet Explorer.
Cross-Browser Compatibility
Impressive Webs has a great outline of Internet Explorer’s getElementById() problem. Essentially, if you have an HTML element like this:
<span name="exampleName"></span>
…then, in most browsers, document.getElementById("exampleName") will not give you the span in question, but in IE7, it will. (Other browsers could use document.getElementsByName("exampleName")[0] to return this particular span.)
This means that, to be consistent across browsers (and assuming we can’t change the HTML), we’ll need to write code like this:
var theSpan;
if (usingIE) { //I won't explain how to detect the browser here
var temp = document.getElementById("exampleId");
//this might have returned an element with a name of "exampleId", so we must check:
if (temp.getAttribute("id") == "exampleId") {
theSpan = temp;
}
}
else {
theSpan = document.getElementById("exampleId");
}
More generally, we could wrap that up into a re-usable function:
function getElById(id) {
if (usingIE) {
var temp = document.getElementById(id);
if (temp.getAttribute("id") == id) {
return temp;
}
}
else {
theSpan = document.getElementById(id);
}
}
Great! But, unfortunately, there are so many of these irritating little differences; it will probably surprise you, if you’re coming from a straight-Flash background, where “compatibility issue” generally means that the Flash Player is a little slow on certain platforms.
jQuery solves this. It papers over the cracks between different browsers with its own set of functions, so if the user’s browser is at least as new as IE6, your code can have a consistent interface.
That’s not the only way that jQuery makes JavaScript simpler…
Easy Syntax
Let’s go back to this snippet of HTML, and assume we want to retrieve the highlighted p elements from the DOM:
<div>
<span id="example1">
<p>Here's some example text.</p>
<p>More example text that we created on the fly.</p>
</span>
<span id="example2">
<p>A second span.</p>
<p>We don't care about these paragraphs.</p>
</span>
</div>
With jQuery, we can just write:
jQuery("#example1 p")
That’s all we need! #example1 says “get the element with an ID of example1” and p says “get all the p elements that are children of that element”. It returns a “jQuery object”, which is a JavaScript object that contains the two p elements themselves from the JS DOM, plus a few extra properties and methods specific to jQuery.
We can make it even shorter still, by replacing jQuery with $ – there’s no mystery here; $ is just a short variable name. Compare it to native JavaScript code:
//regular JS, without cross-browser compatibility:
document.getElementById("example1").getElementsByTagName("p")
//jQuery, with cross-browser compatibility built in:
$("#example1 p")
It’s not just shorter, it’s consistent with CSS, which makes it easy to pick up. We could use the exact same selector as inside our jQuery() call to style these specific paragraphs in a CSS stylesheet:
#example1 p { color: red }
That’s just a very simple example; I’m not going to go into detail, but I’m sure you can see the benefit of being able to use the same selectors in both CSS and jQuery.
More Than Just Selection
I mentioned that the jQuery objects returned by a $() call had additional methods and properties. These give you an easy syntax for writing other common pieces of code.
For example, we could add a mouse click event listener and handler function to both of those p elements like so:
$("#example1 p").click(function() {
alert("You clicked a paragraph!");
});
Or, you could make them all invisible:
$("#example1 p").hide();
Or, you could run some more general JavaScript on them:
var allText = "";
$("#example1 p").each(function() {
allText += $(this).text();
});
In each case, jQuery provides a simple, short, consistent way of writing. It means it’s faster to get code from your head to the browser – and not just because it requires fewer characters.
Tweens and Transitions
Since Flash has its roots in animation, we’re used to it having plenty of tweening capabilities built in – both in the Flash IDE and in the Tween class, not to mention the various tweening libraries available.
JavaScript is different; animation is not an intrinsic part of the platform. But little tweens and transitions are expected parts of the user interaction of modern web app: if I add a new comment on a thread, it slides into place; if I remove an item from my shopping basket, it flashes red and disappears. Without these, apps look unpolished.
jQuery adds these little transitions.
For example, let’s say we want to make either of the aforementioned paragraphs fade out when they’re clicked. Easy:
$("#example1 p").click(function() {
$(this).fadeOut();
});
Nothing to it! And you can pass a duration and callback to the fadeOut() function, if you don’t like the defaults.
For something a little more powerful, we can use the animate() method. This is essentially the equivalent of a tween; pass it a set of CSS properties and values to animate towards, a duration, a type of easing, and a callback, and it takes care of it all for you.
It’s not exactly Greensock, but it’s far more convenient than writing these effects from scratch, and ideal for web app interfaces.
Speaking of which…
UI Widgets
HTML has a few UI components built in, of course: buttons, text fields, and so on. HTML5 defines a few new ones, like a pop-up calendar picker and color picket (though these are currently only supported in Opera).
But these aren’t enough, on their own, to make up a full, modern web app interface. There’s nothing to handle tabs within a page, or a progress bar, or even a simple dialog window (outside of alert() and prompt()).
jQuery UI, an optional library build on top of jQuery, adds these extra widgets, with methods and events that are consistent with the usual jQuery syntax. Think of it as a JavaScript equivalent to Keith Peters’ AS3 MinimalComps. The demo page shows it off better than I can explain it.
Every widget can support custom themes, so you can create a single skin that fits your site and apply it to every component, making it possible to modify their appearance all at once. Again: consistency! Plus, you can apply UI-related effects to other elements; make it possible for the user to drag or resize a DOM element, or click and drag a box around a group of elements to select them for submission.
Other Reasons jQuery is So Popular
The cross-browser compatibility, easy syntax, tween capabilities, and UI elements are the main benefits of jQuery over plain JS in my eyes. There are other reasons to like it, though:
- It’s widely-used, and has been around for six years: it’s not some flash-in-the-pan “new hotness” that’s still unproven and might just die off in a few months. You can trust that it’ll be around for a while.
- There’s a big community surrounding it: this means there are plenty of tutorials and books and forums and people to learn from; you’re not going to be fumbling around in the dark.
- The documentation is excellent: seriously, take a look; it’s very clean and full of examples and demos
- There are even alternative sets of docs with different interfaces, if that’s what you require (another example of the great community)
- It’s open source: the community can add to it, and you can hack on it and learn from it
- Paul Irish has two videos breaking down what he learned from just running through the source
So why wouldn’t you use jQuery? As with most technology choices, it’s a question of making sure you’re using the right tool for the job. If you have a simple DOM structure, or don’t require fancy animations and transitions, or are mainly using the canvas to render your interface, rather than widgets, jQuery probably isn’t necessary.
If you’re already using one or more JavaScript libraries – Dojo, MooTools, YUI, etc. – you may well find that you don’t need jQuery’s functionality on top of what they offer. But my goal in this article is not to try to sell you on any particular library above any other.
I hope this article has helped to explain just what the big deal is about jQuery, particularly if you’re coming from the world of AS3 and Flash. If you’d like to learn it, check out the jQuery tag over on Nettuts+ – jQuery for Absolute Beginners is a good place to start.
Let me know if you’ve any questions!



View full post on Activetuts+
Jan 25, 2012
Posted on Jan 25, 2012 in Hints and Tips | 10 comments
Hey Flash developers! In this tutorial series we are going to go through the process of developing a very basic Tower Defense game. In this first part of the series, we’ll learn how to deploy turrets on the game field, give them the ability to aim at an object (in this case, the mouse) and make them fire particles.
Final Result Preview
Once we complete this tutorial, we are going to have this:
Click a circle to mount a turret on it. Notice how all the turrets rotate so that they point towards the mouse cursor. Click, and all mounted turrets will fire a particle towards the cursor.
Step 1: What is a Tower Defense Game?
Wikipedia’s definition sums it up nicely:
The goal of tower defense games is to try to stop enemies from crossing a map by building towers which shoot at them as they pass.
That is essentially what we will be developing in this tutorial series. Remember that we refer to the towers as turrets in this tutorial.
Cursed Treasure is a great example of a TD game, if you’re still unsure.
Step 2: Setup – IDE
Before we start developing the game, we need to setup the project on our IDE. I’ll be using FlashDevelop here. If you want to read about how to setup the project on Flash Develop, please have a read Steps 1 and 2 of this tutorial, or this full guide to FlashDevelop.
So now you should have a main class, Main.as, with the following code:
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main():void
{
}
}
}
Step 3: Understand the Game Elements
For this part, our game will have following game elements:
- Game Field: The area where all game elements will be placed.
- Turret Placeholder: This is a place on the game field, defined to hold a turret.
- Turret: Our weapon in the game that can be placed on turret placeholders.
- Bullet: And finally, the particles that the turrets fire.
All the above elements will be created in the Main.as except the turrets which will be a separate Turret class.
Lets start coding now!
Step 4: Creating the Placeholders
First we’ll create a function called createTurretPlaceholder() which will create and return us a placeholder sprite. Add the following to the Main class:
private function createTurretPlaceholder():Sprite {
var placeholder:Sprite = new Sprite();
// draw the graphics
var g:Graphics = placeholder.graphics;
g.beginFill(0xCE7822);
g.drawCircle(0, 0, 20);
g.endFill();
return placeholder;
}
This function is simply creating a Sprite variable placeholder. Then using Actionscript drawing API we create the graphic, which is a simple circle. Finally it returns that sprite.
Step 5: Adding Some Placeholders
Now we’ll create three placeholders using the previous function and put them at different positions on the field. Add the following code in the Main() constructor:
var placeholder1:Sprite = createTurretPlaceholder();
In the above statement we create a variable placeholder1 of type Sprite which receives a placeholder from the above createTurretPlaceholder() function.
placeholder1.x = 200; placeholder1.y = 60;
We position the placeholder on the field.
addChild(placeholder1);
And then we add the placeholder to the stage.
Using the same code, we’ll add two more placeholders to the field – so your Main() function should look like this:
public function Main() {
var placeholder1:Sprite = createTurretPlaceholder();
placeholder1.x = 200; placeholder1.y = 60;
var placeholder2:Sprite = createTurretPlaceholder();
placeholder2.x = 60; placeholder2.y = 260;
var placeholder3:Sprite = createTurretPlaceholder();
placeholder3.x = 350; placeholder3.y = 260;
addChild(placeholder1);
addChild(placeholder2);
addChild(placeholder3);
}
Step 6: Turret – Creating the Class
As I mentioned before, the turret is going to be a separate class. This is because turrets need to have specific properties and methods of their own, and to be extended in future to create different type of turrets. This makes them a perfect candidate to be defined in a separate class.
Go on and create a new class called Turret, derived from Sprite, in a file named Turret.as. It should have the following basic code:
package
{
import flash.display.Sprite;
public class Turret extends Sprite
{
public function Turret()
{
}
}
}
Step 7: Turret – The Graphics
Now that we have base structure of the Turret class, the next step is to give the turret some graphics. For that we create a new function called draw() in the class. So put the following function just below the constructor:
private function draw():void {
var g:Graphics = this.graphics;
g.beginFill(0xD7D700);
g.drawCircle(0, 0, 20);
g.beginFill(0x800000);
g.drawRect(0, -5, 25, 10);
g.endFill();
}
As you might have noticed in the code, we draw a circle and a rectangle on it. That’s how our turret is going to look. Now we call this function from the constructor itself.
public function Turret()
{
draw();
}
Step 8: Creating a Ghost Turret
The next thing we do is to display a ghost turret when we hover the placeholders that we have put on the game field. What is a ghost turret? Well, its just a transparent turret that appears when hovering the mouse on the placeholders, to tell the player that a turret can be deployed there.
To begin with this, we need a ghost turret. Go ahead and declare a variable for it in the Main class.
private var ghost_turret:Turret;
Now create a new turret in the Main() constructor:
ghost_turret = new Turret();
Give the ghost turret some properties so that it looks like a ghost:
ghost_turret.alpha = 0.5;
ghost_turret.mouseEnabled = false;
ghost_turret.visible = false;
In the above code we lower down the opacity of the turret to half (0.5) and set the mouseEnabled property of the turret to false so that the ghost turret does not receive any mouse events. Why? We will see that later. And as the ghost turret will be invisible by default, we hide it.
Finally, add the turret to the display list:
addChild(ghost_turret);
Your Main constructor should look something like this:
public function Main() {
var placeholder1:Sprite = createTurretPlaceholder();
placeholder1.x = 200; placeholder1.y = 60;
var placeholder2:Sprite = createTurretPlaceholder();
placeholder2.x = 60; placeholder2.y = 260;
var placeholder3:Sprite = createTurretPlaceholder();
placeholder3.x = 350; placeholder3.y = 260;
addChild(placeholder1);
addChild(placeholder2);
addChild(placeholder3);
ghost_turret = new Turret();
ghost_turret.alpha = 0.5;
ghost_turret.mouseEnabled = false;
ghost_turret.visible = false;
addChild(ghost_turret);
}
If you run the movie now (Ctrl+Enter), all you’ll see is three placeholders on the stage. Boring huh? Lets add some interactivity.
Step 9: Showing/Hiding the Ghost Turret
We want the ghost turret to appear when the mouse hovers over any placeholder. So let’s attach mouse listeners to each placeholder in the createTurretPlaceholder() function just before it returns the placeholder variable.
private function createTurretPlaceholder():Sprite {
var placeholder:Sprite = new Sprite();
// draw the graphics
var g:Graphics = placeholder.graphics;
g.beginFill(0xCE7822);
g.drawCircle(0, 0, 20);
g.endFill();
placeholder.addEventListener(MouseEvent.MOUSE_OVER, showGhostTurret, false, 0, true);
placeholder.addEventListener(MouseEvent.MOUSE_OUT, hideGhostTurret, false, 0, true);
return placeholder;
}
The code attaches listeners to MOUSE_OVER and MOUSE_OUT events.
Next we define the two handler functions for the same. Add the following two functions below the createTurretPlaceholder() function:
private function showGhostTurret(e:MouseEvent = null):void {
var target_placeholder:Sprite = e.currentTarget as Sprite;
ghost_turret.x = target_placeholder.x;
ghost_turret.y = target_placeholder.y;
ghost_turret.visible = true;
}
private function hideGhostTurret(e:MouseEvent = null):void {
ghost_turret.visible = false;
}
hideGhostTurret() just hides the ghost turret but whats going on in the showGhostTurret function? Lets see.
var target_placeholder:Sprite = e.currentTarget as Sprite;
We get the reference of the placeholder on which mouse is present using the MouseEvent‘s currentTarget property, typecasted to Sprite.
ghost_turret.x = target_placeholder.x;
ghost_turret.y = target_placeholder.y;
ghost_turret.visible = true;
This is simple…we position the ghost turret to the placeholder’s cordinates and make it visible. Run the movie and you should see the ghost turret appear when hovering over the placeholders. Nice!
Step 10: Deploying Turrets
Our next objective is to deploy a turret when a placeholder is clicked. For that we need a CLICK listener on the placeholder. But before that we need an Array variable which will hold all our turrets, so we can reference them at any time later. Make one in the Main class.
private var turrets:Array = [];
Then attach another listener just below the two previous listeners we added in the createTurretPlaceholder() function:
placeholder.addEventListener(MouseEvent.MOUSE_OVER, showGhostTurret, false, 0, true);
placeholder.addEventListener(MouseEvent.MOUSE_OUT, hideGhostTurret, false, 0, true);
placeholder.addEventListener(MouseEvent.CLICK, addTurret, false, 0, true);
Declare the addTurret() handler function below the hideGhostTurret() function:
private function addTurret(e:MouseEvent):void {
}
Now lets write the code for the function. Add the following code to the addTurret() function.
var target_placeholder:Sprite = e.currentTarget as Sprite;
We first get the reference to the placeholder that was clicked just like we did in the showGhostTurret() function.
var turret:Turret = new Turret();
The we create a new turret in a variable named turret.
turret.x = target_placeholder.x;
turret.y = target_placeholder.y;
Next, we position the turret at the coordinates of the target_placeholder.
addChild(turret);
turrets.push(turret);
When the turret is created, we add it to the stage and push it onto the array.
Your addTurret function should look like this in the end:
private function addTurret(e:MouseEvent):void {
var target_placeholder:Sprite = e.currentTarget as Sprite;
var turret:Turret = new Turret();
turret.x = target_placeholder.x;
turret.y = target_placeholder.y;
addChild(turret);
turrets.push(turret);
}
One thing to note here. Remember we set the mouseEnabled property of the ghost turret to false? If we hadn’t, the ghost turret in between the placeholder and mouse would have captured the click event, thus preventing the event from reaching the placeholder. As a result the CLICK listener attached to the placeholder would not have get called.
You might want to remove the CLICK listener from the placeholder. I haven’t done it here since the new turret blocks any clicks, but if you use a different turret design, it’s a good idea.
Well thats all we need to place our turrets. Try running the movie and you should be able to deploy turrets on placeholders.
Step 11: Making the Turret Move
For this tutorial we will make the turret rotate to face the mouse. We’ll keep the rotation functionality in a separate method of the Turret class. This method will be called update().
Lets add this method to Turret now:
public function update():void {
var angle:Number = Math.atan2(stage.mouseY - this.y, stage.mouseX - this.x) / Math.PI * 180;
this.rotation = angle;
}
All we do in ths function is calculate the angle of the mouse from the turret and set the turret’s angle to it. (Take a look at Trigonometry for Flash Developers if you’re not sure how this works.)
But notice that we have not called this function from anywhere and so nothing happens yet. This function will be called from the game loop that we define next. Lets go!
Step 12: The Game Loop
Whats a game loop? Its a function that is attach to the ENTER_FRAME event and so it gets called on every frame. It updates all the elements in the game…in our case, Turrets. More details in this article.
Create a function called gameLoop() below the Main() constructor which will be a listener to the ENTER_FRAME event of the movie:
private function gameLoop(e:Event):void {
}
Now that we have the listener function defined, we need to attach it to the corresponding event. We do so in the Main() constructor. Add the following line to the Main() function:
public function Main() {
var placeholder1:Sprite = createTurretPlaceholder();
placeholder1.x = 200; placeholder1.y = 60;
var placeholder2:Sprite = createTurretPlaceholder();
placeholder2.x = 60; placeholder2.y = 260;
var placeholder3:Sprite = createTurretPlaceholder();
placeholder3.x = 350; placeholder3.y = 260;
addChild(placeholder1);
addChild(placeholder2);
addChild(placeholder3);
ghost_turret = new Turret();
ghost_turret.alpha = 0.5;
ghost_turret.mouseEnabled = false;
addChild(ghost_turret);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
}
Step 13: Updating All the Turrets
Lets put some code in our gameLoop() function.
for each(var turret:Turret in turrets) {
}
We iterate over the turrets array, which has references to all turrets on the stage, using a for each...in loop.
for each(var turret:Turret in turrets) {
turret.update();
}
And call the update() function of each turret. And we are done. If you run the movie now, you should be able to deploy turrets which always face the mouse. Something like this:
Step 14: Making the Turrets Shoot
Our next objective is to make the turrets shoot bullets. At whom? For this tutorial, we’ll make the turrets shoot towards any point we click on the stage. We’ll do this like so:
- Add a click listener to the stage.
- Iterate over all the turrets in the above listener.
- Calculate the angle from the clicked point to the turret.
- Create a new bullet and move it in the appropriate direction.
Lets declare the listener function named shoot Add the function to Main class:
private function shoot(e:MouseEvent):void {
}
And then attach the above listener to the CLICK event of stage in the Main() constructor:
stage.addEventListener(MouseEvent.CLICK, shoot);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
}
Step 15: Creating the Bullet
Before we proceed to writing the code for shooting in the shoot() function, we will define a new function for creating a bullet, just like we did for creating a placeholder. So put the following function below the createTurretPlaceholder():
private function createBullet():Sprite {
var bullet:Sprite = new Sprite();
// draw the graphics
var g:Graphics = bullet.graphics;
g.beginFill(0xEEEEEE);
g.drawCircle(0, 0, 5);
g.endFill();
return bullet;
}
Nothing much here. We just create a new Sprite, draw an off-white color circle inside it, and return it. Now let’s continue to define our shoot() function.
Step 16: How to Shoot?
Time to add some code to the shoot().
for each(var turret:Turret in turrets) {
}
First, we iterate over all the turrets on the stage using the for each...in loop.
var new_bullet:Sprite = createBullet();
Now for every turret we create a bullet using the function we created previously.
new_bullet.rotation = turret.rotation;
Here we store the value of turret’s rotation property in bullet’s rotation. Why? Well…rotating the bullet is not what we want. The bullet needs to continue moving in direction determined by the turret, and for this we need the turret’s rotation value from the time it shot the bullet. We just store this as the bullet’s rotation for future use.
new_bullet.x = turret.x + Math.cos(new_bullet.rotation * Math.PI / 180) * 25;
new_bullet.y = turret.y + Math.sin(new_bullet.rotation * Math.PI / 180) * 25;
These two lines set the bullet’s initial position, which is 25 pixels away from the turret in the direction of facing (remember the rotation property). Again, read up on trigonometry if this is unfamiliar to you.
addChild(new_bullet);
And as the usual last step, we add the bullet to the stage’s display list.
This is how your shoot() function should look like:
private function shoot(e:MouseEvent):void {
for each(var turret:Turret in turrets) {
var new_bullet:Sprite = createBullet();
new_bullet.rotation = turret.rotation;
new_bullet.x = turret.x + Math.cos(new_bullet.rotation * Math.PI / 180) * 25;
new_bullet.y = turret.y + Math.sin(new_bullet.rotation * Math.PI / 180) * 25;
addChild(new_bullet);
}
}
If you run you game now and click on any placeholder, the turret will get get deployed – but oops… we have a problem here. An extra bullet also gets created with the turret. Lets fix this.
Step 17: Why Did That Extra Bullet Appear?
To understand this we need to understand event propagation in AS3.
Any event which gets generated passes through three phases: capturing, target and bubbling. The events starts from the topmost parent of the target which generated the event. It passes through all the inner child elements, which is the capturing phase. Then it reaches the actual target which is the target phase. Then the event goes back to the top, passing through same elements in reverse, which is the bubbling phase. In the bubbling phase, all the elements which have a listener defined for the propagating event get triggered and their listeners are executed.
We have a CLICK listener bound to the stage and the placeholders. But the placeholders are also children of the stage. So when we click the placeholder, a CLICK event gets generated with the target as the placeholder. The event propagates from the stage towards the placeholder – the capture phase. It reaches the placeholder and its CLICK handler, the addTurret() function, gets executed and we have a turret on the stage. Now the event propagates backwards – the bubbling phase – and when it reaches the stage again it finds a CLICK listener for it as well, which gets executed. As a result the shoot() function gets executed and a bullet is added to the stage.
So thats the problem, but how to solve it? What we need to do is stop the event’s further propagation when it reaches the target. That means in the addTurret() we stop the event’s propagation. So go ahead and add a line at the end of addTurret():
private function addTurret(e:MouseEvent):void {
var target_placeholder:Sprite = e.currentTarget as Sprite;
var turret:Turret = new Turret();
turret.x = target_placeholder.x;
turret.y = target_placeholder.y;
addChild(turret);
turrets.push(turret);
e.stopPropagation();
}
The line we added stops the event’s propagation and it doesn’t reaches the stage. For a more comprehensive understanding of event framework in Actionscript 3.0 read the AS3 101 post. Let’s continue with the game.
Step 18: Making the Bullet Move
We create the bullet on clicking the stage but it doesn’t move still. Our next step is to add an event listener to the bullet which gets called on every frame and moves it. First, we declare a variable for the bullet’s speed. Add the variable where the other variables are declared:
private var ghost_turret:Turret;
private var turrets:Array = [];
private var bullet_speed:uint = 3;
Then, add the following listener function to the Main class:
private function moveBullet(e:Event):void {
var bullet:Sprite = e.currentTarget as Sprite;
bullet.x += Math.cos(bullet.rotation * Math.PI / 180) * bullet_speed;
bullet.y += Math.sin(bullet.rotation * Math.PI / 180) * bullet_speed;
}
What we do in this listener is:
- Get the reference of the bullet whose listener was triggered.
- Increment the bullet’s position by
bullet_speed amount in the direction of the bullet’s rotation.
Lastly, attach the listener we just created to the ENTER_FRAME event of the bullet in the shoot() function:
private function shoot(e:MouseEvent):void {
for each(var turret:Turret in turrets) {
var new_bullet:Sprite = createBullet();
new_bullet.rotation = turret.rotation;
new_bullet.x = turret.x + Math.cos(new_bullet.rotation * Math.PI / 180) * 25;
new_bullet.y = turret.y + Math.sin(new_bullet.rotation * Math.PI / 180) * 25;
new_bullet.addEventListener(Event.ENTER_FRAME, moveBullet, false, 0, true);
addChild(new_bullet);
}
}
If you test you game now, you should see bullets moving when you click on the stage. But if you look carefully, you will notice that the bullets keep moving once created…even after they go out of the visible stage area. Our next step is to destroy the bullets as soon as they leave the movie boundaries.
Step 19: Destroying the Bullets
Add the following code in the end of moveBullet() function:
private function moveBullet(e:Event):void {
var bullet:Sprite = e.currentTarget as Sprite;
bullet.x += Math.cos(bullet.rotation * Math.PI / 180) * bullet_speed;
bullet.y += Math.sin(bullet.rotation * Math.PI / 180) * bullet_speed;
if (bullet.x < 0 || bullet.x > stage.stageWidth || bullet.y < 0 || bullet.y > stage.stageHeight) {
bullet.removeEventListener(Event.ENTER_FRAME, moveBullet);
bullet.parent.removeChild(bullet);
bullet = null;
}
}
Here we check whether the bullet is out of the stage boundaries. If it is, we remove its ENTER_FRAME listener and remove the actual bullet from the stage. We also set the bullet variable to null so that the bullet has no reference left and is available for garbage collection.
Conclusion
We have completed our basic tower defense game in which you can deploy turrets on specific placeholders which shoot towards any point we click on the stage. Cool, huh?
In the next part we’ll add enemies to the game and intelligence to the turrets so that they can do what they are suppose to do: defend. Also we’ll add some more elements that make the game look more complete and cool. Till then, try adding some more features to this on your own.



View full post on Activetuts+
Jan 25, 2012
Posted on Jan 25, 2012 in Hints and Tips | 10 comments
It’s Premium tutorial time! This week, Tuts+ members will learn how to use the EaselJS JavaScript library (along with SoundJS and TweenJS) to create a version of the classic Pong game in HTML5.
Premium Preview

Click to play the demo
In this tutorial, we’ll create a clone of the classic game, Pong, in HTML5, using the EaselJS library. The game will have multiple screens, sound effects, and a (very simple) AI opponent.
Read the Full Tutorial
Premium members can access the full tutorial right away!
If you’re not yet a Premium member, you can still read the first few steps of the tutorial.
Tuts+ Premium Membership
We run a Premium membership system which periodically gives members access to extra tutorials, like this one! You’ll also get access to Psd Premium, Vector Premium, Audio Premium, Net Premium, Ae Premium, Cg Premium, Photo Premium, and the new Mobile Premium too. If you’re a Premium member, you can log in and download the tutorial. If you’re not a member, you can of course join today!
Also, don’t forget to follow @envatoactive on twitter, circle us on Google+, like us on Facebook, and grab the Activetuts+ RSS Feed to stay up to date with the latest tutorials and articles.



View full post on Activetuts+
Jan 23, 2012
Posted on Jan 23, 2012 in Hints and Tips | 10 comments
There are a lot of games out there with jerky, unrealistic movements and that can do only one thing to your product: make it unappealing to the audience. But smooth movement is not hard to achieve – let’s get to work!
Final Result Preview
Let’s take a look at the final result we will be working towards:
Step 1: Set Up the Environment
This is a straight forward tutorial, so the setting up will also be straight forward.
Create a New ActionScript 3.0 Flash Project. The stage size and color don’t matter, just use what you are confortable with.
I use FlashDevelop for coding, but also this could be done in any AS editor, like Flash Pro (or any text editor, maybe Notepad
). So, create a Class file, make sure your code looks pretty much like mine; see below. I called mine “Movement”. (If you’re using Flash Pro, check out this guide to creating a class.)
package {
import flash.display.Sprite;
public class Movement extends Sprite {
public function Movement():void {
}
}
}
After you’re done, make sure your Class is linked to the Flash project as the Main Class.
Step 2: Create the Square and Variables
So after you’ve linked the Movement Class to your document, define the variables as I did below
package {
import flash.display.Sprite;
import flash.events.Event;
public class Movement extends Sprite {
//The object that will move
private var square:Sprite;
//The maximum speed
private var _max:Number = 10;
//The variables that are going to be applied to move the square
private var dx:Number = 0;
private var dy:Number = 0;
public function Movement():void {
//Listen for added to stage event
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
//Creating a new Sprite and draw inside a square
square = new Sprite();
square.graphics.beginFill(0x333333);
square.graphics.drawRect(0, 0, 30, 30);
square.x = stage.stageWidth / 2 - square.width / 2;
square.y = stage.stageHeight / 2 - square.height / 2;
addChild(square);
}
}
}
This is pretty much all that we’ll do for creating the object. You can use your own object but for this simple movement tutorial I used this simple square.
Step 3: Introducing the Input.as Class
Hi guys, this is the Input.as Class; Input.as Class these are the guys I told you about – be nice to them!
So what is this class about, you may wonder. Basically it does your key handling job for you. It adds a listener to ENTER_FRAME events – with low priority – and a key listener which fills some private Dictionaries. Also it uses another Class for key codes. You can take a look inside and see for yourself how is working.
Note: The Input.as Class does not belong to me. It was created by Matthew Bush, who ported Box2D to Flash.
//Example of Input.as Class usage
//You have to always initialize it as this with the stage parameter
Input.initialize(stage);
//After initializing, you can use kd(), kp() or ku() methods, which
//return a Boolean value if the conditions are met.
//These methods accept multiple arguments,
//so for one event you can use multiple keys.
//This makes it a lot easier to give a boost of accessibility to your app.
//e.g See below as I use one call for detecting UP arrow or W for going up.
Input.kd("UP", "W");
Step 4: Importing the Classes
So now that you are familiar with the Input.as Class, we are going to import it in our Movement Class and initialize it.
package {
import flash.display.Sprite;
import flash.events.Event;
import Input;
public class Movement extends Sprite {
//The object that will move
private var square:Sprite;
//The maximum speed
private var _max:Number = 10;
//The variables that are going to be applied to move the square
private var dx:Number = 0;
private var dy:Number = 0;
public function Movement():void {
//Listen for added to stage event
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
//Creating a new Sprite and draw inside a square
square = new Sprite();
square.graphics.beginFill(0x333333);
square.graphics.drawRect(0, 0, 30, 30);
square.x = stage.stageWidth / 2 - square.width / 2;
square.y = stage.stageHeight / 2 - square.height / 2;
addChild(square);
//Initialize the Input.as Class with handler on stage
Input.initialize(stage);
//Add the refresh loop
addEventListener(Event.ENTER_FRAME, refresh);
}
private function refresh(e:Event):void {
}
}
}
Step 5: Handling the Key Inputs
I use an ENTER_FRAME-based loop for detecting the key inputs; below is the refresh() method which is the handler function for this event.
private function refresh(e:Event):void {
//Key Handler
if (Input.kd("A", "LEFT")) {
//Move to the left
}
if (Input.kd("D", "RIGHT")) {
//Move to the right
}
if (!Input.kd("A", "LEFT", "D", "RIGHT")) {
//If there is no left/right pressed
}
if (Input.kd("W", "UP")) {
//Move up
}
if (Input.kd("S", "DOWN")) {
//Move down
}
if (!Input.kd("W", "UP", "S", "DOWN")) {
//If there is no up/down action
}
}
Step 6: Explaining the Calculations – Handling the Velocity
This is pretty straight forward. Detect whether any of the keys are pressed, then act accordingly.
I use the ternary operator a lot: value = condition ? true : false;
This is basically an if-statement that’s been condensed to a single line.
For every key detection, I use this method: if the value is bigger than _max then set it equal to _max; otherwise, increment or decrement that particular value as appropriate. This way, it’s kept within certain bounds. Simple, right?
Below you can study the conditions:
private function refresh(e:Event):void {
//Key Handler
if (Input.kd("A", "LEFT")) {
//Move to the left
dx = dx < 0.5 - _max ? _max * -1 : dx - 0.5;
}
if (Input.kd("D", "RIGHT")) {
//Move to the right
dx = dx > _max - 0.5 ? _max : dx + 0.5;
}
if (!Input.kd("A", "LEFT", "D", "RIGHT")) {
//If there is no left/right pressed
if (dx > 0.5) {
dx = dx < 0.5 ? 0 : dx - 0.5;
} else {
dx = dx > -0.5 ? 0 : dx + 0.5;
}
}
if (Input.kd("W", "UP")) {
//Move up
dy = dy < 0.5 - _max ? _max * -1 : dy - 0.5;
}
if (Input.kd("S", "DOWN")) {
//Move down
dy = dy > _max - 0.5 ? _max : dy + 0.5;
}
if (!Input.kd("W", "UP", "S", "DOWN")) {
//If there is no up/down action
if (dy > 0.5) {
dy = dy < 0.5 ? 0 : dy - 0.5;
} else {
dy = dy > -0.5 ? 0 : dy + 0.5;
}
}
//After all that, apply these to the object
square.x += dx;
square.y += dy;
}
If you’re unfamiliar with the ternary operator, grab a piece of paper and a pen and write out a few of them in if…else format; it’s a great exercise to get to grips with that’s going on.
Keep in mind I manipulate the dx and dy variables, and only set the actual x and y values at the end. This helps us make the motion fluid; it’s not jerking around as we alter their values directly throughout the function..
Go on, test it! See how nicely it’s moving?
Step 7: Handling Boundary Collisions
Okay. Everything is right, moving fluidly – but OUT of the stage! Below I added the collision detection conditions.
private function refresh(e:Event):void {
//Key Handler
if (Input.kd("A", "LEFT")) {
//Move to the left
dx = dx < 0.5 - _max ? _max * -1 : dx - 0.5;
}
if (Input.kd("D", "RIGHT")) {
//Move to the right
dx = dx > _max - 0.5 ? _max : dx + 0.5;
}
if (!Input.kd("A", "LEFT", "D", "RIGHT")) {
//If there is no left/right pressed
if (dx > 0.5) {
dx = dx < 0.5 ? 0 : dx - 0.5;
} else {
dx = dx > -0.5 ? 0 : dx + 0.5;
}
}
if (Input.kd("W", "UP")) {
//Move up
dy = dy < 0.5 - _max ? _max * -1 : dy - 0.5;
}
if (Input.kd("S", "DOWN")) {
//Move down
dy = dy > _max - 0.5 ? _max : dy + 0.5;
}
if (!Input.kd("W", "UP", "S", "DOWN")) {
//If there is no up/down action
if (dy > 0.5) {
dy = dy < 0.5 ? 0 : dy - 0.5;
} else {
dy = dy > -0.5 ? 0 : dy + 0.5;
}
}
//Boundary detection
if (square.x - dx < 0 || square.x + dx + square.width > stage.stageWidth) {
//x axis detection
}
if (square.y - dy < 0 || square.y + dy + square.height > stage.stageHeight) {
//y axis detection
}
//After all that, apply these to the object
square.x += dx;
square.y += dy;
}
It’s looking for boundaries in a more precise fashion, by checking whether the edges of the square hit the boundaries (before this, it was just checking the center of the square against the boundaries).
Great. Now we need to add the code to make the square bounce off the boundaries. What I do for that is multiply by -1 the axis value dx or dy. But that is not enough! If the speed is quite fast, then the square will get through the margins or just go nuts. So before we multiply we need to set the x or y of the object to be the same as the boundary that it meets.
So if x < 0 (and so it is colliding with the left edge), then we move the object to be exactly on the left edge, like so: object.x = 0; and then multiply the dx by -1.
//Margin detection
if (square.x - dx < 0 || square.x + dx + square.width > stage.stageWidth) {
//x axis detection
square.x = square.x - dx < 0 ? 0 : stage.stageWidth - square.width;
dx *= -1;
}
if (square.y - dy < 0 || square.y + dy + square.height > stage.stageHeight) {
//y axis detection
square.y = square.y - dy < 0 ? 0 : stage.stageHeight - square.height;
dy *= -1;
}
Test it now! Bouncy right?
To make it even better, go on experimenting with different values - like instead of multiplying by -1, try -0.7 and see the results.
Conclusion
So you met the Input.as Class, got to know how to work with it, and made a nice fluid movement in just a few minutes. I think this counts as a great time!
Please leave your comments below and any other questions, I will gladly answer.
But if you encounter any problem please check twice your code, compare it with the source file and then if you can't make it work, feel free to post a question.



View full post on Activetuts+
Jan 22, 2012
Posted on Jan 22, 2012 in Hints and Tips | 10 comments
As HTML games begin to gradually increase in popularity, vendors are starting to introduce some exciting new APIs to make gaming that little bit sweeter for both us developers and our end players. One of these is the GamepadAPI, which allows you to connect your good old console gamepad into your computer and use it for browser based games, plug and play style. Let’s dive in!
Introduction: What Is the Gamepad API?
In a nutshell, the Gamepad API allows you to interact with your browser using a video game console controller, AKA a gamepad. This doesn’t require a special driver or plugin to work, it’s as simple as plug and play!
Being a console gamer rather than a desktop gamer myself, I much prefer to interact with games using a gamepad, and with the upcoming rise of HTML and JavaScript based games, this is going to become a really useful tool in making games more easily accessible for your users.
The Gamepad API is not readily available for public release, but we can start using it for ourselves with preview versions of Firefox. So before we get stuck in, we need a few things.
What You’ll Need
As I mentioned, the Gamepad API isn’t available for public release just yet so you will need to first get yourself a Nightly build of Firefox and make sure you have the Firebug add-on installed (for debugging purposes only).
Also, you can’t forget a gamepad! I’m going to be using a PlayStation 3 controller for this tutorial but an Xbox controller will do just fine.
Once you have installed Nightly and added on Firebug you are ready to go!
(NB. Recent builds of Chromium have Gamepad API support as well, but this tutorial has not been tested against them.)
Step 1: Connecting a Gamepad to Your Browser
Let’s start with a basic HTML file (index.html), sourcing “gamepad.js” (a blank JavaScript file).
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Introduction to the Gamepad API</title>
</head>
<body>
<h1>Gamepad API</h1>
<script src="gamepad.js"></script>
</body>
</html>
The connection of a gamepad is detected with a simple JavaScript event listener, the event fired is called “MozGamepadConnected”. So the first thing we need to do is add an event listener to the window to detect that event.
I’m also adding a callback function that will log the details of the event to Firebug’s console. This is the information we are most interested in and what will actually let us know that we have connected a gamepad successfully.
function gamepadConnected(evt)
{
console.log(evt);
}
window.addEventListener('MozGamepadConnected', gamepadConnected);
Run your index.html in Nightly and open up Firebug’s console, here we’ll be able to see the logging of the event from our callback function.
Make sure your controller is turned off and not connected wirelessly to a games console. Plug it in to your computer via USB and power on the controller, watching the event log in the console.

Great, we have a gamepad connecting to a browser, no extra plugins or drivers required!
Step 2: Disconnecting a Gamepad
It’s just as important to know whether a gamepad has been disconnected as well, so let’s look at the event, “MozGamepadDisconnected”.
Similarly to step one, add an event listener for a disconnect event and a callback function to log the event details.
function gamepadDisconnected(evt)
{
console.log(evt);
}
window.addEventListener('MozGamepadDisconnected', gamepadDisconnected);
If you’re gamepad is still connected, refresh your page (which you’ll see connected event be logged) and then disconnect your gamepad by ejecting it from the USB port. You should get an event log like this one.

Now we know when a gamepad has been connected and disconnected, it’s probably a good idea to record the state inside a variable and get ready to detect button events!
var gamepadActive = false;
function gamepadConnected(evt)
{
console.log(evt);
gamepadActive = true;
}
function gamepadDisconnected(evt)
{
console.log(evt);
gamepadActive = false;
}
window.addEventListener('MozGamepadConnected', gamepadConnected);
window.addEventListener('MozGamepadDisconnected', gamepadDisconnected);
Step 3: Detecting Button Presses
Button presses, again, use an event listener and callback function with two events, “MozGamepadButtonDown” and “MozGamepadButtonUp”.
I would suggest logging the entire event from the button press yourself to see what is going on, but the key piece of information we need to get from this event is evt.button. This is the numerical id of the button that was pressed.
The callback function this time takes a second parameter, a boolean value to test if the button was pressed or released. We set this ourselves in the callback functions of the event listeners.
function buttonPressed(evt, pressed)
{
console.log(evt.button, pressed);
}
window.addEventListener("MozGamepadButtonDown", function(evt) { buttonPressed(evt, true); } );
window.addEventListener("MozGamepadButtonUp", function(evt) { buttonPressed(evt, false); } );
This should now output the IDs of the buttons that are pressed and whether they were pressed or released (true for button down, false for button up).

Next we’ll create an array with all the PlayStation 3 buttons in. The indices of the array will map to the IDs used on this gamepad, with the values being the name of the button.
var gamepadActive = false,
ps3Buttons = new Array();
ps3Buttons[12] = 'triangle',
ps3Buttons[15] = 'square',
ps3Buttons[14] = 'cross',
ps3Buttons[13] = 'circle',
ps3Buttons[4] = 'up',
ps3Buttons[7] = 'left',
ps3Buttons[6] = 'down',
ps3Buttons[5] = 'right',
ps3Buttons[10] = 'L1',
ps3Buttons[8] = 'L2',
ps3Buttons[11] = 'R1',
ps3Buttons[9] = 'R2',
ps3Buttons[1] = 'L3',
ps3Buttons[2] = 'R3',
ps3Buttons[16] = 'PS',
ps3Buttons[0] = 'select',
ps3Buttons[3] = 'start';
If you’re using a different controller, take the time to figure out which index goes with which button, and store that info in a similar array.
If we now modify the buttonPressed() function ever so slightly, we can easily tell which button on the controller has been pressed.
function buttonPressed(evt, pressed)
{
console.log(ps3Buttons[evt.button] + ' was pressed');
}
Give it a go! Pressing buttons on your controller should now log the name of buttons being pressed. This will be a lot easier to understand than “button 5″ (which, in my case, is on the D-pad).
Step 4: Detecting Axis Events
Detecting axis events is basically keeping track of where the left and right analog sticks on the gamepad are positioned using the “MozGamepadAxisMove” event.
Add the new event handler and callback function.
function moveAnalogSticks(evt) {
console.log(evt.axis, evt.value);
}
window.addEventListener("MozGamepadAxisMove", moveAnalogSticks);
This is what we get – confusing, right?

There is only one event fired by both analog sticks; each event gives us one of four possible axis and a value between -1.0 and +1.0. Axis 0 and 1 belong to left analog stick and axis 2 and 3 belong to the right.

In the diagram above you’ll see axis 0 and 2 correspond to the x axis, and 1 and 3 correspond to the y axis. By using both the x and y axis for each individual analog stick, you can figure out which way the analog stick is facing!
On different gamepads, you may have other axes. For instance, the shoulder triggers on an Xbox controller are also analog.
Step 5: Putting It Into Practice
That covers all of the events that we can currently take from a gamepad, so let’s put what we’ve learnt into practice.
Now, I don’t want to go too heavily into the game development side of things, as we are focusing on what we use to control games themselves. One of the key things to look at, though, is switching control schemes. As not everyone will have a gamepad ready to hand, we need to make sure we provide controls for both the keyboard and gamepad.
Step 6: Setting Up Your Canvas
To get a small demo up and running, create a canvas element in your html file with an id of “game” and set the width to 600 and height to 540. As you may know, the canvas element is commonly used to render HTML games on.
You will also want to copy the “ship.png” and “space.jpg” images from the source download to your working folder as these are what we’ll be rendering to the canvas. Alternatively, find some graphics of your own to have a play with!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Introduction to the Gamepad API</title>
</head>
<body>
<h1>Gamepad API</h1>
<canvas id="game" width="600" height="540"></canvas>
<script src="gamepad.js"></script>
</body>
</html>
Step 7: Creating the Game Loop
Now that the canvas element is in our DOM, we want to create a game loop to render our game.
I’m using a shim for “requestAnimationFrame” by Paul Irish that will be the base for our loop. Next, we get the 2D context of the canvas which we’ll use to draw on and create two new image objects, one for the background and one for our spaceship.
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.getElementById('game'),
ctx = canvas.getContext('2d'),
ship = new Image(),
space = new Image();
space.src = "space.jpg";
ship.src = "ship.png";
Next, the player object. It has x and y coordinates which keep track of where it should appear on the canvas; four direction states (up, down, left and right) so we can know which way the ship is moving; a render() function, which first calls updatePosition() and then draws the image of the ship onto the canvas based on the x and y coordinates, and finally the updatePosition() function itself, which tests to see which way the ship is set to move and updates its position accordingly.
var player = {
x: 200,
y: 250,
up: false,
down: false,
left: false,
right: false,
render: function() {
this.updatePosition();
ctx.drawImage(ship,this.x,this.y);
},
updatePosition: function() {
this.up ? this.y-- : false;
this.down ? this.y++ : false;
this.left ? this.x-- : false;
this.right ? this.x++ : false;
}
}
After that we have our “renderGame” function which draws the space background image onto the canvas first, then draws our spaceship on top of that.
And finally, our loop. This function calls itself again and again, each time calling our “renderGame” function.
function renderGame()
{
ctx.drawImage(space,0,0);
player.render();
}
;(function animloop(){
requestAnimFrame(animloop);
renderGame();
})();
Your canvas should now have a nice space looking background with a spaceship sat in the middle of it – not too exciting, I know. So let’s add some controls!
Step 8: Hooking Up the Ship’s Controls
In our player code we named the four buttons which we want to control our ship with. These match up to the names of the buttons inside the ps3Buttons[] array. So, all we have to do is modify our buttonPressed() function ever so slightly and we’ll be moving.
var player = {
...
up: false,
down: false,
left: false,
right: false,
...
}
Now when a gamepad button is pressed or released it will set its state within the player object, so when the “up” button is pressed, player.up = true/false will be set.
function buttonPressed(evt, pressed)
{
console.log(evt.button, pressed);
player[ps3Buttons[evt.button]] = pressed ? true : false;
}
Head back over to your demo and you should be able to move your ship around!
Step 9: Adding a Keyboard Fallback
As not everyone playing your game will have a gamepad, you’ll probably still want to allow them to play the game with a keyboard.
Lets first create a new keys[] array, and map the keyboard’s arrow keys’ keyCode properties to the equivalent buttons on the gamepad. This will allow us to reuse buttonPressed() function that the gamepad utilises.
var gamepadActive = false,
ps3Buttons = new Array(),
keys = new Array();
ps3Buttons[12] = 'triangle',
ps3Buttons[15] = 'square',
ps3Buttons[14] = 'cross',
ps3Buttons[13] = 'circle',
ps3Buttons[4] = 'up',
ps3Buttons[7] = 'left',
ps3Buttons[6] = 'down',
ps3Buttons[5] = 'right',
ps3Buttons[10] = 'L1',
ps3Buttons[8] = 'L2',
ps3Buttons[11] = 'R1',
ps3Buttons[9] = 'R2',
ps3Buttons[1] = 'L3',
ps3Buttons[2] = 'R3',
ps3Buttons[16] = 'PS',
ps3Buttons[0] = 'select',
ps3Buttons[3] = 'start';
keys[38] = 4;
keys[37] = 7;
keys[40] = 6;
keys[39] = 5;
Now we need a “onkeyup” and “onkeydown” event listener for the arrow keys. When a key is pressed or released, we make sure that a gamepad is not in use. Then we prevent the arrow key from doing its usual task (scrolling the browser window up or down in this case) and then call the same buttonPressed() function that the gamepad calls.
To do this, a fake event object is passed with the key’s “keyCode” mapped to an item in the keys[] array, which in turn, passes the corresponding gamepad button ID.
window.onkeydown = function(evt)
{
if (gamepadActive == false)
{
evt.preventDefault();
buttonPressed({ button: keys[evt.keyCode] }, true);
}
}
window.onkeyup = function(evt)
{
if (gamepadActive == false)
{
evt.preventDefault();
buttonPressed({ button: keys[evt.keyCode] }, false);
}
}
This should now let you use the arrow keys for controlling the ship when a gamepad isn’t plugged in, while still letting the gamepad take over when it’s present.
Conclusion
So we’ve covered the basics of connecting a gamepad to your computer, learnt how to hook into the events that the gamepad fires, and then use them in practice. Not forgetting, the crucial fall-back support for the keyboard!
A quick challenge for those of you with a controller other than a PS3 Dual Shock: adjust the button mapping based on whichever controller is plugged in.
Thank you for taking the time to learn about the Gamepad API. If you have any questions, please leave them in the comments.



View full post on Activetuts+
Page 2 of 23712345...102030...»Last »