Tutorial: Create a Brick Breaker Game in AS3
Table of Contents
Step 1: Coding Paddle Movement
Ok, this is going to be my first tutorial on hopefully a simple subject. This is probably going to be broken up into a few parts. So, here we go.
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:
//First I defined a function where all of
//the code needed to start the game is placed
//This includes listeners, variable definitions, and other stuff
function beginCode():void{
//Adds a listener to the paddle which
//runs a function every time a frame passes
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
}
function movePaddle(event:Event):void{
//The paddle follows the mouse
mcPaddle.x = mouseX;
}
beginCode();
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 = mouseX;
with
mcPaddle.x = mouseX - mcPaddle.width / 2;
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 movePaddle function.
//If the mouse goes off too far to the left
if(mouseX < mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = 0;
}
//If the mouse goes off too far to the right
if(mouseX > stage.stageWidth - mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = stage.stageWidth - 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.
//First I defined a function where all of
//the code needed to start the game is placed
//This includes listeners, variable definitions, and other stuff
function beginCode():void{
//Adds a listener to the paddle which
//runs a function every time a frame passes
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
}
function movePaddle(event:Event):void{
//The paddle follows the mouse
mcPaddle.x = mouseX - mcPaddle.width / 2;
//Keeping the paddle in the stage
//If the mouse goes off too far to the left
if(mouseX < mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = 0;
}
//If the mouse goes off too far to the right
if(mouseX > stage.stageWidth - mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = stage.stageWidth - mcPaddle.width;
}
}
beginCode();
[…] Coding Paddle Movement […]
July 30th, 2008 at 9:47 am
[…] breaker game tutorial:http://www.mrsunstudios.com/2008/07/tutorial-create-a-brick-breaker-game-in-as3/ It’s a really bad one because you end up with […]
May 22nd, 2009 at 5:10 pm