Creating a Flash Lite Game - Page 3
Pages 1, 2, 3, 4, 5In this stage we will create the Game Initialization Frame, the code in this frame runs only once to create all the objects we need for the game, it also creates the starting values for variables in the game such as the game score and the number of lives the player has. We will start with the different types of content we have from the bottom up, the background is already done from the first frame, so we will move straight into the Main Object layer.
Creating the Player and the Target Objects
Our Main Contents layer will host the Player and the Target that will be caught during the game. Assuming that you have already unzipped the file given in the previous page. Import the included player.swf movie into your library (File>Import>Import to Library). Open the Library (Ctrl+L) and drag an instance of your player onto the second frame of our Main Contents layer. The location of the player object on stage does not matter because we will it later position using ActionScript. We have to convert this graphic to a Movie Clip Symbol to do that though. So select it, and then press F8 to convert it to a Symbol. Assign the name as MyPlayer, set the type to Movie Clip, and then change the Registration Point to the top left corner as indicated in the image below.

Before we forget, you have to also assign the instance name player_mc to this movie clip using the Properties Inspector.

Our game is going to have three targets falling at the same time, we will create these three targets using ActionScript, but we need to have the source template for the target from which we will duplicate our additional copies. You will have to import the target.swf file included in the zip file you downloaded earlier, go through File>Import>Import to Library to do just that, then open the library (Ctrl+L) and drag a copy of it onto the Main Contents layer. You have to position this object somewhere above the stage because we do not need to see it during run time. Convert to a symbol (F8), set the registration point to the top left corner, name the symbol MyTarget. Click OK and then assign the instance name target_mc to it.

Adding the Score and number of Lives display
The player needs to see the number of points he scored and how many lives he has remaining. We will do that by creating the text labels and then a couple of dynamic text fields to show the actual numbers. Start off by using the Text Tool to write two texts that say score and lives, make sure that the text type of each of these texts is static (check the Properties Inspector).

We will now create the dynamic texts that will actually display the numbers for each of these variables. Make sure that no text is selected, pick the Text Tool, access the Properties Inspector and set the text type to Dynamic. Now draw a text field next to score, set the font to _sans, font size to 11, and align the text to the left. You will now have to assign the variable name score to it to display that variable when it is created.
Create another field next to the text that says lives, use the same properties as above and assign the variable name lives to it. You should end up with a screen similar to the one below.
That completes all the visual content of this frame, we will skip the Button layer because there are no navigation commands in this frame. We will move to the Actions now.
ActionScript
Select the Actions layer, right-click the second frame and select Actions. (Might need to use the trick mentioned in the previous page regarding Frame selection for ActionScript). Paste the code below to initialize the game assets. The code is pretty simple. The first two lines are the basic variables which we are using, the score (starts at zero) and the lives (player has three lives). we are setting the _x and _y properties of the player_mc object, and then duplicating the target_mc three times, the actual target_mc will not be used again. Each of the new targets will be set at their position horizontally, and will be placed at a random position above the screen before falling, I used a random value here to make sure that they don't all fall together making it hard for the player to catch all three objects at the same instance.
score=0;
lives=3;
//Positions the player on the stage.
player_mc._x=0;
player_mc._y=280;
//Creates 3 targets by duplicating the one on stage duplicateMovieClip("target_mc","target1_mc",1); duplicateMovieClip("target_mc","target2_mc",2); duplicateMovieClip("target_mc","target3_mc",3);
//Positions the new targets above the stage ready to play
target1_mc._x=0;
target1_mc._y=-random(40);
target2_mc._x=80;
target2_mc._y=-random(40);
target3_mc._x=160;
target3_mc._y=-random(40);
We would like our targets to fall at different speeds, so we will assign a different speed property to each of these targets. The speed property will be used when the game actually runs, but we have to assign it now. The initial speed will be a random number between 0 and 10. If the speed is actually zero, the targets will not move, so we will be adding the number 4 to whatever speed we get. We could have used 1 instead of 4, but playing at speed 1 is way too boring. The code was changed this way:
score=0;
lives=3;
//Positions the player on the stage.
player_mc._x=0;
player_mc._y=280;
//Creates 3 targets by duplicating the one on stage duplicateMovieClip("target_mc","target1_mc",1); duplicateMovieClip("target_mc","target2_mc",2); duplicateMovieClip("target_mc","target3_mc",3);
//Positions the new targets above the stage ready to play
target1_mc._x=0;
target1_mc._y=-random(40);
target1_mc.speed=random(10)+4;
target2_mc._x=80;
target2_mc._y=-random(40);
target2_mc.speed=random(10)+4;
target3_mc._x=160;
target3_mc._y=-random(40);
target3_mc.speed=random(10)+4;
We need to create a variable to store the location of the player on the stage and to check for it at the time the objects fall, we can recall the position at run time, but checking for a number like that can be messy, to have a nicer and a more understandable code we are going to create a property called myPosition to tell us if the player is at position 1 (the left), position 2 (the center), or position 3 (the right). At the start, the player is as the left (_x=0), so his position is 1. This will make much more sense by the end of the next frame.
//Essential Variables
score=0;
lives=3;
//Positions the player on the stage.
player_mc._x=0;
player_mc._y=280;
player_mc.myPosition=1;
//Creates 3 targets by duplicating the one on stage
duplicateMovieClip("target_mc","target1_mc",1);
duplicateMovieClip("target_mc","target2_mc",2);
duplicateMovieClip("target_mc","target3_mc",3);
//Positions the new targets above the stage ready to play
target1_mc._x=0;
target1_mc._y=-random(40);
target1_mc.speed=random(10)+4;
target2_mc._x=80;
target2_mc._y=-random(40);
target2_mc.speed=random(10)+4;
target3_mc._x=160;
target3_mc._y=-random(40);
target3_mc.speed=random(10)+4;
Initialization is now done. In this stage, we did the following:
- Added the player object to the Main Contents layer.
- Added the target object to the Main Contents layer.
- Added the score and live displays in the Main Contents layer.
- Assigned the Actions to create the essential variables.
- Assigned the Actions to position the player.
- Assigned the Actions to duplicate the target.
- Assigned the Actions to position and configure these new targets.
Glad you have made it to this frame, the main Game Execution loop Frame is next.