Tutorials
FILE CATEGORIES
Tutorials >> Pong
Posted by fflash
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Monitoring keyboard events in AS3
- More drawing functions
- Controlled responses to keyboard events.
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. The first thing to do is to create a new project with one .fla and an ActionScirpt File. Save the ActionScript file as Game.as. Then set the document class of the fla to "Game". Then set the Frame Rate of the .fla to about 40. Now, finally, we can crack on with code. First comes the package declaration and then the imports; today we will be using Movie Clips, Events, and a new one, the "Keyboard" class. This Keyboard class allows us to utilise keyboard events, the focus of today's tutorial.
Next come the definition of a Movie Clip, which will hold the player. Then we move onto the constuctor function. This is the first block of our code.
If the key that has just been pressed is the 'up' key
and we are not touching the top or above
move up 5 pixels
Instead if the key that has just been pressed is the 'down' key
and we are not touching the bottom or below
move down 5 pixels
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file
So what exactly have we made?
Well, today, we have made a white rectangle can move up and down, but only to the edge of the screen. Next time we will add in the ball (quite a complex thing). That will soon exist.
PART 2
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Apply math(s) (which I'm not going to explain) in AS3
- Bouncing effect at top and bottom of screen
- Applying hitTestObject function.
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. Today we will be creating the ball. First we need to add a load of variables and a load of objects. The first is the ball Movie Clip (pretty obvious one, that one). The rest concern the angle at which the ball rebounds off of the paddle, and its speed afterwards. The idea is something like this diagram shows, if you can work out what exactly it does show:

Okay then. Here are all of those aforementiong variables, which you need to paste underneath the var playerone:MovieClip line:
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file.
So what exactly have we made?
Well, today, we have made a ball, and made it bounce off the top, bottom and our paddle correctly. Next time we will add in the enemy (so we can have a rally).
PART 3
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Reversing the math(s) which we did last week for use with the AI as well
- Adding a "brain" for the AI
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. Today we will be creating the AI - the opposition. First we need to add the Movie Clip object for the AI, next to all the other similar declarations, near the top of the code:
"...the two prefixed with "u" refer to "you" so we will be using them today, and not the ones prefixed with "e" ("enemy")..."
Well today we are going to use these one prefixed with "e". We are, in short, going to do the opposite of what we did last week. To do this, we are going to add in another if statement , this time hittesting the AI MC; like the last, very similar one, anywhere inside the ballmovement function. It looks like this:
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file. You should be able to have a bit of the rally before the ball gets stuck off the screen.
So what exactly have we made?
Well, today, we have made the ball bounce off the AI paddle correctly. Next time we will make the ball return when it goes off the screen and score the game.
PART 4
A full version of the souce code created in this part in also available. In this final part of the project, you will learn:
- How to score a simple game by setting a textbox to a value
- Resetting the game
- Quick function creation
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. The first thing you need to do is to create a couple of textboxes to hold the scores. You can do this using the "text" tool from the toolbox. Make them dynamic textboxes by using the drop down box in the properties box that will probably say "Static Text" at the moment. You need to set their instance names to scoreText1 (player score) and scoreText2 (enemy score).
For more information about dynamic textboxes and instance names, see a previous tutorial
Good. Once you've done that, you need to add this line of code into the Game() function near the top of our code:
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file. Hurrah! It should all work. If it doesn't work for you, a line may have been missed by me (you never know).
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Monitoring keyboard events in AS3
- More drawing functions
- Controlled responses to keyboard events.
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. The first thing to do is to create a new project with one .fla and an ActionScirpt File. Save the ActionScript file as Game.as. Then set the document class of the fla to "Game". Then set the Frame Rate of the .fla to about 40. Now, finally, we can crack on with code. First comes the package declaration and then the imports; today we will be using Movie Clips, Events, and a new one, the "Keyboard" class. This Keyboard class allows us to utilise keyboard events, the focus of today's tutorial.
Next come the definition of a Movie Clip, which will hold the player. Then we move onto the constuctor function. This is the first block of our code.
- Code: Select all
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
public class Game extends MovieClip {
public var playerone:MovieClip = new MovieClip;
public function Game() {
- Code: Select all
stage.focus = this;
- Code: Select all
playerone.graphics.beginFill(0xffffff);
playerone.graphics.drawRect(0,0,20,100);
playerone.x = 10;
playerone.y = 140;
addChild(playerone);
- Code: Select all
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
}
public function onKeyboardDown(event:KeyboardEvent):void {
- Code: Select all
if (event.keyCode == Keyboard.UP) {
if (playerone.y > 0) {
playerone.y -= 5;
}
} else if (event.keyCode == Keyboard.DOWN) {
if (playerone.y < 300) {
playerone.y +=5;
}
}
}//End function
}//End class
}//End package
If the key that has just been pressed is the 'up' key
and we are not touching the top or above
move up 5 pixels
Instead if the key that has just been pressed is the 'down' key
and we are not touching the bottom or below
move down 5 pixels
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file
So what exactly have we made?
Well, today, we have made a white rectangle can move up and down, but only to the edge of the screen. Next time we will add in the ball (quite a complex thing). That will soon exist.
PART 2
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Apply math(s) (which I'm not going to explain) in AS3
- Bouncing effect at top and bottom of screen
- Applying hitTestObject function.
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. Today we will be creating the ball. First we need to add a load of variables and a load of objects. The first is the ball Movie Clip (pretty obvious one, that one). The rest concern the angle at which the ball rebounds off of the paddle, and its speed afterwards. The idea is something like this diagram shows, if you can work out what exactly it does show:

Okay then. Here are all of those aforementiong variables, which you need to paste underneath the var playerone:MovieClip line:
- Code: Select all
public var ball:MovieClip = new MovieClip;
public var xspeed:Number = -0.5;
public var yspeed:Number = 0;
public var udist:Number = 0;
public var ufraction:Number = 0;
public var edist:Number = 0;
public var efraction:Number = 0;
public var fullspeed:Number = 10;
public var maxspeed:Number = 5;
- Code: Select all
playerone.graphics.endFill();//If you haven't already got this line, I forgot it last time
ball.graphics.beginFill(0xffffff);
ball.graphics.drawCircle(270,190,20);
ball.graphics.endFill();
ball.addEventListener(Event.ENTER_FRAME,ballmovement);
addChild(ball);
- Code: Select all
public function ballmovement(e:Event):void {
if (xspeed < maxspeed && xspeed >= 0) {
xspeed += 0.05;
}
if (xspeed > -maxspeed && xspeed < 0) {
xspeed -= 0.05;
}
ball.x += xspeed;
ball.y += yspeed;
- Code: Select all
if (ball.y>=((stage.stageHeight/2)-10)&&yspeed>0) {
//If hitting bottom
yspeed = -yspeed;//Go other direction
}
if (ball.y<=-((stage.stageHeight/2)-30)&&yspeed<0) {
//If hitting top
yspeed = -yspeed;//Go other direction
}
if (ball.hitTestObject(playerone)) {//if ball hits player paddle
udist=(playerone.y) - (ball.y + 5);
//Calculate the xspeed and yspeed needed for correct angle and set them to that value.
if (udist!=0) {
ufraction=udist/(playerone.height/2);
yspeed=-((1-Math.abs(ufraction))*fullspeed);
var oldxspeed = xspeed;
xspeed=ufraction*fullspeed;
if (xspeed < oldxspeed) {
xspeed = oldxspeed;
yspeed *= (oldxspeed / xspeed);
}
} else if (udist==0) {
xspeed=-fullspeed;
}
//
if (xspeed<0) {
xspeed=-xspeed;//Make sure we are going in the right direction...
}
if (xspeed > maxspeed) {
xspeed = maxspeed;//...but not too fast
}
}//End hittest
}//End function
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file.
So what exactly have we made?
Well, today, we have made a ball, and made it bounce off the top, bottom and our paddle correctly. Next time we will add in the enemy (so we can have a rally).
PART 3
A full version of the souce code created in this part in also available. In this part of the project, you will learn:
- Reversing the math(s) which we did last week for use with the AI as well
- Adding a "brain" for the AI
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. Today we will be creating the AI - the opposition. First we need to add the Movie Clip object for the AI, next to all the other similar declarations, near the top of the code:
- Code: Select all
public var ai:MovieClip = new MovieClip;
- Code: Select all
ai.graphics.beginFill(0xffffff);
ai.graphics.drawRect(520,140,20,100);
ai.graphics.endFill();
ai.addEventListener(Event.ENTER_FRAME,aimovement);
addChild(ai);
- Code: Select all
public function aimovement(e:Event):void {
if (ball.y>ai.y){
ai.y += 6;
}
if (ball.y<ai.y){
ai.y -= 6;
}
}
"...the two prefixed with "u" refer to "you" so we will be using them today, and not the ones prefixed with "e" ("enemy")..."
Well today we are going to use these one prefixed with "e". We are, in short, going to do the opposite of what we did last week. To do this, we are going to add in another if statement , this time hittesting the AI MC; like the last, very similar one, anywhere inside the ballmovement function. It looks like this:
- Code: Select all
if (ball.hitTestObject(ai)) {//if ball hits AI paddle
edist=(ai.y + 5)-(ball.y + 5);
if (edist!=0) {
efraction=edist/(ai.height/2);
yspeed=-((1-Math.abs(efraction))*fullspeed);
xspeed=efraction*fullspeed;
} else {
xspeed=fullspeed;
}
if (xspeed>0) {
xspeed=-xspeed;//Make sure we are going in the right direction...
}
if (xspeed > maxspeed) {
xspeed = maxspeed;//... and not too fast, either
}
}
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file. You should be able to have a bit of the rally before the ball gets stuck off the screen.
So what exactly have we made?
Well, today, we have made the ball bounce off the AI paddle correctly. Next time we will make the ball return when it goes off the screen and score the game.
PART 4
A full version of the souce code created in this part in also available. In this final part of the project, you will learn:
- How to score a simple game by setting a textbox to a value
- Resetting the game
- Quick function creation
Here is a live example of what we will create.
Over the course of the series you will make the game below:

Okay, let's get started. The first thing you need to do is to create a couple of textboxes to hold the scores. You can do this using the "text" tool from the toolbox. Make them dynamic textboxes by using the drop down box in the properties box that will probably say "Static Text" at the moment. You need to set their instance names to scoreText1 (player score) and scoreText2 (enemy score).
For more information about dynamic textboxes and instance names, see a previous tutorial
Good. Once you've done that, you need to add this line of code into the Game() function near the top of our code:
- Code: Select all
stage.addEventListener(Event.ENTER_FRAME,resetHandler);
- Code: Select all
public function resetHandler(e:Event){
if(ball.x < (-270)){
- Code: Select all
scoreText2.text = String(Number(scoreText2.text ) + 1)
reset();
}
- Code: Select all
if(ball.x > (550 - 270)){
scoreText1.text = String(Number(scoreText1.text ) + 1)
reset();
}
}
- Code: Select all
function reset(){
xspeed = -0.5;
yspeed = 0;
udist = 0;
ufraction = 0;
edist = 0;
efraction = 0;
fullspeed = 10;
maxspeed = 5;
ball.y = 0;
ball.x = 0;
playerone.y = 140;
}
It is, of course, CTRL/Command and ENTER to view our work, once you have saved the .as file. Hurrah! It should all work. If it doesn't work for you, a line may have been missed by me (you never know).

Twitter