How to make a simple AS 3.0 Pong Game (Tutorial)

First of all, I’m so so sorry for my long delay, I had a tough period, with lots of hard exams and stuff like that, but now I’m coming with a new tutorial, that I hope will be helpful for many of you . In the beginning I’ll show you how the final result should look.

Final Result Preview

Create the objects

Paddle.as

So, in Flash Builder or in other IDE create a new AS 3.0 Project and name it PongGame.
In this first step we will create our objects, and because this is a simple game, we have only two objects: a paddle and a ball. First let’s see how the Paddle class looks like.

I know it is much information at once, but I don’t think it is so hard to understand because this class holds the graphic representation of our ball, and all the logical stuff that our object needs to know. Don’t get freaked out, all the methods that are unclear for you right now, you will understand them when we reach the main class.
Let’s talk about this class and all the methods that are inside it:

First we have 6 variables, that are only about our ball, like: radius, alpha, color , speed, VX, VY. Then we have 2 variables that are not about our ball, but are related to ball position : enemyPoints, heroPoints.

Next it is a simple constructor, and of course a Draw(); method again, because we need to give shape to our object. SetBallStartPosition(); takes the object and places it at the center of the screen.

Now, we have a getRandom(); method and a MoveBall(); method. As you may assume the first method < getRandom(); > , returns us a random number from an interval. This number will direct, our ball movement, as you can see in the next method < MoveBall(); >. Update(); function moves the ball on the screen.

setWallColision(); and setPaddleCollision();, these two methods, take care that when the ball touches the paddle or the edge of the screen, it goes in the opposite direction.

The last two functions, return the number of hero points, and the number of the enemy points.

So, this is the Hero class.SetHeroStartPosition() – place the object in the area where it should be.Update() – Calls three methods : onKeyDown, onKeyUp and movePaddle. The first and the second method, check if one of the UP and DOWN buttons are pressed. And the movePaddle() just move our object on the screen.SetLimits() – don’t allow the object to exceed the screen height.

Again the first method that we meet is SetEnemyStartPosition, I think it is clear what this function does. Next we have the Update method; here is where the logical stuff about how our enemy should move on the screen, happen . And of course the final method is SetLimits, as I said before, this function don’t allow the object to exceed the screen height.

In this class, I’ve created only graphical elements that are not moving, like the score and the fillet. I think this is the easiest class to understand, and because of that, I will not stay longer to explain here, because this contains only graphics elements and some attributes.

Here we are at the end of the tutorial, this is the final class and the most important one, because here are put together all the methods and logical stuff that we created so far.

As you can see the first three variables are: hero, enemy and ball; these are our most important objects. Then we have two variables for hero points and for enemy points and of course the last four variables are describing the game graphic that is not moving.

In the constructor we have two methods : LoadContent and an enter frame function called Update.

In the LoadContent (the name says it all) , we loaded all the graphic elements that we have( hero, enemy, ball, aso. ). I do not know if you have noticed, but the LoadContent function calls another function named StartGame. This new method place the objects on the screen.

Now we are at the most important function of the game, I named it Update. Why is it the most important function of the game?. Because it takes care about all the events that are happening. It knows what to do in every situation, of course if the developer takes care to cover all the possibilities.
As you can see, inside of this method are called some other methods that I spoke about earlier.

And the final method is UpdateScore, this method counts the hero points and the enemy points.

So, this was my tutorial about How to make a simple AS 3.0 Pong Game . I hope you have understood all the things that I tried to explain to you. I wish this tutorial will be helpful for many of you out there.Cya next time.