Tutorial: Create a Brick Breaker Game in AS2
Categories: AS2, All Tutorials, Beginner Tutorials, Flash, Game Development
Table of Contents
Step 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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; } } |





Good Tutorial! It was chosen for the home page of http://www.tutorialsroom.com
Waiting for your next tutorial :)
January 11th, 2009 at 3:28 pm
Tutorial added!
http://www.tutorialand.com
January 12th, 2009 at 6:35 am
[...] Create a Brick Breaker Game in AS2 [...]
February 11th, 2009 at 1:26 pm
is my movie suppost to look like your when im done copying the code because it doesnt movie im using flash mx is that the problem
March 17th, 2009 at 8:43 am
not working in flash mx, why?
March 30th, 2009 at 9:08 am
The problem probably isn’t that you’re using flash mx.
March 31st, 2009 at 12:55 am
ok…i am a total boom. i cannot even change the background color! omg…
April 13th, 2009 at 1:00 pm
ok.. i did it.. now how to get the code into the actions layer?
April 13th, 2009 at 1:05 pm
Code isnt Working..Paddle Movement
May 13th, 2009 at 8:08 am
This coding does not work in mx 2004. And it’s very aggravating. Is there anyway to fix it? The paddle just won’t stay on the stage.
May 18th, 2009 at 12:26 pm
hi, i like this tutorial so much….
to anonymous, i think u should change ur flash to Flash8..
im looking forward to make a game!!!
May 24th, 2009 at 8:17 am
Hi, i followed what you’ve said to knowledge, but my mcPaddle doesn’t move.
I have these errors;
Discription; Statement block must be terminated by ‘}’
Source; if(_xmouse > Stage.width – mcPaddle._width / 2){
Discription; Syntax error.
Source; N/A
Discription; Statement block must be terminated by ‘}’
Source; onEnterFrame = function(){ //this function will run during every single frame
Discription; Syntax error.
Source; N/A
New to flash, so don’t really knpw what this means.
Regards,
Tim
May 29th, 2009 at 6:47 pm
Tim:
use this:
if(_xmouse > Stage.width – mcPaddle._width/2){
code goes here
}
don’t for get the other brace to tell flash that the if statement is over.
oh, and to the author:
instead of that code to center the paddle w/ the mouse, you can just set the registration point of the paddle to the center when you create it.
June 9th, 2009 at 8:39 pm
I have flash 8 and no matter what I do to the code the paddle refuses to stay on the stage
June 17th, 2009 at 8:48 am
I have CS4 and I can’t even get the first peace of code to work this is the error I keep getting.
Statement must appear within on/onClipEvent handler
and thats for line one.
July 10th, 2009 at 3:28 pm
is this correct:
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 Stage.width – mcPaddle._width / 2){
//Keep the paddle on stage
mcPaddle._x = Stage.width – mcPaddle._width;
}
}
coz if it is nothing happens anybody know why?
July 12th, 2009 at 1:36 am