NEW! FLASH Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, Flash CS3 and more.

Creating a Flash Lite Game - Page 4

Pages 1, 2, 3, 4, 5

The Game Execution loop Frame holds the code that runs the game, controls the objects, counts the score and takes away lives when you fail to catch the object. Our frame will also configure the controls for moving the player on the screen, we will start with this last part first. We have already created and aligned all our visual objects in our previous frame so we can move straight into the stage to create the button that captures key presses and then code the game loop.

Creating the Game Controls

We need to create the button to hold the control code, select the Buttons layer, open up the library (Ctrl+L) and drag an instance of the MyButton symbol we created earlier, make sure that it placed somewhere above the stage so that it is not visible during the game.

Flash Lite Game - Insert an instance of MyButton symbol

Right-click this instance of the button on the stage and select Actions. We are going to use two buttons only to control the player, we are going to use a very simple conditional to move the player and change the value of its myPosition variable depending on where it currently is.

on (keyPress "<Left>"){
if (player_mc.myPosition != 1){
player_mc.myPosition--;
player_mc._x-=80;

}
}

on (keyPress "<Right>"){
if (player_mc.myPosition != 3){
player_mc.myPosition++;
player_mc._x+=80;

}
}

The code is simple, as long as the position of the player is not 1 (absolute left), then pressing the left key button moves it to the left by 80 pixels, and as long as the position of the player is not 3 (absolute right), then pressing the right key button moves it to the right by 80 pixels. The player movie clip is exactly 80 pixels in width, which is also exactly 1/3 of the width of the stage, so moving it by 80 pixels to the left or the right is a whole step to the left or the right.

I find using the myPosition property easier because I do not have to think about the actual positions when making my conditionals, although those would work the same if you use the exact values. We will also be using this variable later on below to check if the targets are caught.

Coding the Game loop

We have to assign our biggest chunk of code to Frame 3 of the Actions layer. Select that frame and open the Actions panel. We are going to do this in segments, I will explain each one as we go through. Most of our code will be duplicated three times, each for one of the targets. Our first task is to make our objects fall down, we do that by increasing the _y property of each target, we will add to them the value of the speed property we set in the previous frame. Each object has a different speed value, so they should be falling at different speeds.

//Commands the targets to move downwards.
target1_mc._y += target1_mc.speed;
target2_mc._y += target2_mc.speed;
target3_mc._y += target3_mc.speed;

The next task is to configure what happens when the balls reach the ground without being caught. We know that the height of the stage is 320 pixels, so if the _y property exceeds that the target has fallen down. If that is true, then the player loses a life, and then the ball is thrown up to a random place above the stage (that's the new _y is negative), and the speed is set as a random value between zero and the current score (plus 4 to avoid zero speed). This makes the game become faster as you score more. You paste the following below the first segment:

//The targets fall down.
if (target1_mc._y>320){
lives--;
target1_mc._y =-random(40);
target1_mc.speed= random(score)+4;
}
if (target2_mc._y>320){
lives--;
target2_mc._y =-random(40);
target2_mc.speed= random(score)+4;
}
if (target3_mc._y>320){
lives--;
target3_mc._y =-random(40);
target3_mc.speed= random(score)+4;
}

Now what happens if our targets are actually caught? First, each target will be caught under a different condition, each will have to have its _y property lower than 250, and then the player has to be at the corresponding position as the target. 1 for the absolute left, 2 for the middle object, and 3 for the object on the absolute right. If the capture condition for any of the targets is satisfied, the players gains one extra score point, and then the target is thrown up at a random position with random speed to start again.

//The targets are caught.
if (target1_mc._y>250 and player_mc.myPosition == 1 ){
score++;
target1_mc._y =-random(40);
target1_mc.speed= random(score)+4;
}
if (target2_mc._y>250 and player_mc.myPosition == 2 ){
score++;
target2_mc._y =-random(40);
target2_mc.speed= random(score)+4;
}
if (target3_mc._y>250 and player_mc.myPosition == 3 ){
score++;
target3_mc._y =-random(40);
target3_mc.speed= random(score)+4;
}

Our final task is to check for Game Over, if the number of lives reaches zero, then we have to remove all of our targets from the stage and then go to and stop at the Game Over Frame - ie Frame 5.

//Game Over
if (lives == 0){
removeMovieClip("target1_mc");
removeMovieClip("target2_mc");
removeMovieClip("target3_mc");
gotoAndStop(5);
}
We only need to remove objects we created dynamically via ActionScript as those will not be removed when you leave the frame that contains them. The player movie clip does not have to be removed because it is an actual object on the stage and it will not be visible once we leave the frame that contains it.

The loop 2 (Frame 4)

That completes our game engine, but if you noticed, this was all just one frame and to make it loop we will add this code to the next frame, Frame 4 in the Actions layer.

//Plays the previous frame
gotoAndPlay(3);

Our Game loop is done, here is what we did here:

  1. Added a button to the Buttons layer and configured the key controls.
  2. Assigned the Actions to make the targets move.
  3. Assigned the Actions to determine what happens when the targets reach the ground.
  4. Assigned the Actions to determine what happens when the targets are captured.
  5. Assigned the Actions to determine what happens when all the lives are consumed.
  6. Assigned the Actions to make Frame 4 loop with Frame 3. (Game Execution loop).

The very last frame, Game Over Frame, is next.

- Next Page.