Main Content

You are here:

Pong (Part 3)

Welcome to Foundation Flash tutorials. In this series of tutorials you will learn how to make a full game of "Pong!" in AS3. A full version of the souce code created in this part in also available. In this part of the project, you will learn:

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:

public var ai:MovieClip = new MovieClip;

Okay. Now we need to draw in the AI, much as we did with the player Movie Clip. We are also going to add an event listener so the AI is able to respond to events around it. To do this we put these lines in the constructor under the similar lines for the ball and playerone:

ai.graphics.beginFill(0xffffff);
ai.graphics.drawRect(520,140,20,100);
ai.graphics.endFill();
ai.addEventListener(Event.ENTER_FRAME,aimovement);
addChild(ai);

The next logical step, therefore, is to add in this Event Listener. What we want to do is this: when the ball is above the opposition, make it move up; when below the opposition, move down. And we can do this, quite simply, like so:

public function aimovement(e:Event):void {
        if (ball.y>ai.y){
                ai.y += 6;
        }
        if (ball.y<ai.y){
                ai.y -= 6;
        }
}

Now to move onto what to do if the AI hits the ball. To quote last week's tutorial:

...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:

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
        }
}

The comments in that tell you roughly what each bit does. You might want to read the tutorial about the hitTestObject() function if you haven't done so already. All that code is long, I accept, but hey, it makes the ball bounce off the AI and that's all I care about.

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.

See you then!

Harry.

Comments