http://xtutorials.net

Falling Bricks Game - Part 3 PDF Print E-mail
Written by Administrator   
Sunday, 16 September 2007 16:53
This is part 3 of our falling bricks game tutorial. If you did not read the first two parts yet than go and read them. you won't be able to make the game if you did not read those two. Hi and welcome back. This is our third and final part of the tutorial. In this part we are going to focus on the time frame (time you will need to survive in order to finish the level) and we will also ad a secret weapon to the game.

Now let's begin.
first we need to declare a variable that will be the time. Since flash works in frames and not seconds that variable will have to represent the number of frames that need to pass in order to finish the level. To calculate this variable we need to do a simple math calculation. Let's call the time in seconds we want the player to survive as "x", the frame rate of our flash file as "y" and the time in frames as "z". That z=x*y. Simple now? So, for example if we want our player to survive for 20 seconds and our flash frame rate is 60 than the time in frames is 1200.
So go the AS layer and above stop(); write this:
var tTime=1200;

Now we need to create a progress bar that will show exactly how much time has passed. Create a new movie clip and name it progressBar. in it create a 201px empty rectangle. Hight is not important but make it so it will be visible and to fit on top of the game. About 20px would be good.Now create another rectangle (this time a filled one) the same hight as the other one - 1px (if your empty rectangle is 20px than this one should be 19). Width is not important because this one will be scaled dynamically. Convert it to a symbol and name it bar. Place it in the empty rectangle and give the instance name "bar" to it. It should look like this:
After that make it 1px width. The hight should remain the same.
Now place the progressBar MC on the stage and give it the instance name "pBar". Now it is time for the AS.we want every time a frame is passed the progress bar to get a litlle bigger, until it is full. To do that we must first calculate by how much should it grow every frame. We will calculate this when the clip loads. Also we must set the bar width to 0.:
onClipEvent(load){
this.grow = 200/_root.tTime;
this.bar._width = 0;
}
200 is the maximum width of the bar and _root.tTime is the total time we need to spend.
Now we must make it bigger every time until we have reached the total time. This is done every frame:
onClipEvent (enterFrame) {
if (_root.joaca == 1) {
if (this.bar._width<200) {
this.bar._width += this.grow;
} else {
_root.joaca = 0;
}
}
}

Now I told you that we are going to develop a secret weapon. First of all make a nice little animation that should simulate an explosion. Make the last frame of the animation a key frame, because we are going to use some code there. Ad a linkage identifier to it. I named it explosion.
No select the player MC from the stage and go to actions. We need to perform an action when a key is pressed. I chose to perform this action when spacebar is pressed.
oon(keyPress ""){
_root.isHit.attachMovie("explosion", "explosion", 0);
}
NOTE: Is hit is a movieClip on the stage where I load different thinks, including the explosion now. If you don't have it place an empty MC on the stage and name it isHit.
Test your movie and see if it works corectly. Now we also want the game to pause while the explosion is performed. So ad _root.joaca = 0; right after the attachMovie line.
Now it is time to ad code to the explosion. Remember that I told you to make the last frame of the explosion a keyframe? Go to that keyframe. We want it to delete all the bricks on the stage and after that to resume play.
for(i = 0; i<=99; i++){
    unloadMovie("_root.items.item"+i);
}
_root.joaca = 1;
_root.isHit.explosion.unloadMovie();
stop();
now you can just ad a counter to make sure he does not use the bomb all the time. For example he only has three bombs. This is just a simple condition (the code for the player MC on the stage):
on (keyPress "<Space>") {
    if (_root.noBoms>0) {
        _root.isHit.attachMovie("explosion", "explosion", 0);
        _root.joaca = 0;
        _root.bombs -= 1;
    }
}

Now this is the structure of the game. You can ad more elements to it, a score and more levels. Just use your imagination and I am sure that you can make something very good.
You can download the FLA file from our download section and here is how the game should be now (I added a few minor changes):


Last Updated ( Sunday, 16 September 2007 18:04 )