How to Create a Brick Breaker Game in AS2 – Part 1
Part 1: Coding Paddle Movement
Alright, I’ve decided to make a tutorial for you guys on how to create a brick breaker game in ActionScript 2.0. It’ll be very similar to my one for AS3, except coded differently. So, here we go, eh?
First of all, we’re going to make the background of the stage black, simply because it looks better, and we’re going to set the frame rate to 24. Then, we’re going to draw the paddle and we’re going to convert it into a MovieClip.
Mine is 55×10
Next, we’re going to give the paddle an instance name, mcPaddle. The instance name is case sensitive, so type it exactly the way I did.
Now, we get into some basic ActionScript. Create a new layer named “actions” where all of your code will go in. Next, type in this code:
onEnterFrame = function(){ //this function will run during every single frame
//The paddle follows the mouse
mcPaddle._x = _xmouse;
}
Hopefully, I’ve explained enough in the comments for you to get an idea of what the code does. If you test the movie, you might see a few problems. First of all, the paddle is not centered with the mouse, but is left aligned with it. This is easy to fix. Just replace:
mcPaddle._x = _xmouse;
with:
mcPaddle._x = _xmouse - mcPaddle._width*.5;
This code basically makes it so instead of using the paddle’s _x value, which is the left side of the paddle, it uses the middle of the paddle instead to follow the mouse.
Another problem with this code is that the paddle sometimes runs off stage. We don’t want this, it annoys the user. So, we’re going to add the following code to the onEnterFrame function.
//If the mouse goes off too far to the left
if(_xmouse < mcPaddle._width / 2){
//Keep the paddle on stage
mcPaddle._x = 0;
}
//If the mouse goes off too far to the right
if(_xmouse > Stage.width - mcPaddle._width / 2){
//Keep the paddle on stage
mcPaddle._x = Stage.width - mcPaddle._width;
}
This code will keep your paddle in bounds, no matter how large the stage is or how wide your paddle is. I’m leaving the explanation of the code up to you to figure out yourself. After all, you can never become a real programmer if you just copy and paste the code without any idea of what it means.
And now I conclude part one of this tutorial with the final code you should have in your file with an example of what mine looks like.
onEnterFrame = function(){ //this function will run during every single frame
//The paddle follows the mouse
mcPaddle._x = _xmouse - mcPaddle._width*.5;
//If the mouse goes off too far to the left
if(_xmouse < mcPaddle._width / 2){
//Keep the paddle on stage
mcPaddle._x = 0;
}
//If the mouse goes off too far to the right
if(_xmouse > Stage.width - mcPaddle._width / 2){
//Keep the paddle on stage
mcPaddle._x = Stage.width - mcPaddle._width;
}
}