This time we will go into practice. I will teach you how to make a very simple game. Code I will add comments. If you have read the above tutorial, it will not be difficult to fully understand it.
Let's start now. This game is a course-based game that I made when I was a beginner, so I was quite impressed. This game is called snail racing, and the process is like this: There are several tracks on the screen. There is a snail on each track, and each snail crawling speed is different. When the game starts, you have to guess which snail bait runs to the terminal first. If you guessed it, you win, otherwise, game over.
Step 2: set the scene size to 1st * 600 PX and the running speed to 48 frames/second. This is my personal preference.
Step 2: Change the default layer name to bkground and draw the track, start line, and finish line. Here are four tracks.
Step 2: Create a new layer named wn_layer, and draw an interactive snail map to put it on this layer. F8 is defined as MC, and MC is named wn_mc. Put the snail tail in the middle of the MC. If this step is missing, it will end before it reaches the end. Then copy three identical snails and adjust the color in the attribute bar to make them look a little different. Then align to the starting position of each track.
Step 2: Create a layer and name it BTN. Then, create a button and copy the four to put them next to the four snails.
Step 2: Remember the x-axis position data at the beginning of the snail mail. Here I am 25. Drag a snail mail to the end and check its X-axis position data. Then, write down and prepare for the. What I get here is 540. Remember to put it back ~
Step 2: Create a layer named "actoins" for writing. At this layer, we need four key frames. Let me explain why.
We need a frame to prepare to start the game, that is, the frame for players to guess. It is a snail less screen at the beginning of the game. This is completed by 1st frames. The four buttons we make are to let players guess which snail bait will win.
When the game starts, the player cannot change the data. Therefore, when the tortoise crawls, the button layer has no content.
We need to crawl every snail bait once for 2nd frames. Why only let them crawl once? Because when the timeline pointer passes through this key frame, only the as in the frame is executed once. In order to keep the snails crawling, We need to repeatedly let the Time Pointer pass through this 2nd frame, so the content of our 3rd frame is to let the Time Pointer return to 2nd frames.
After the first frame is the frame that shows the result, we will create a new layer named "show". We will make two key frames, 3rd and 4th, and put the words "win" in the second frame, put a failed text in 5th frames.
In addition, after the game is over, we need a button for players to play again. Therefore, a button is required for two frames from the 4th to 5 button layers.
After the layout, the structure of the entire layer (there is no ):
Now we have four objects: _ root. wn1, _ root. wn2, _ root. wn3, _ root. wn4
Next we will make one frame at a time:
Action 1st Frame
Code:
_ Root. Stop (); // stop the timeline
_ Root. truewinner = 0;
_ Root. guesswinner = 0;
The code in the next two sentences defines two variables, truewinner. guesswinner is on the _ root timeline. Variables are used to store data and can be customized. Here, the truewinner and guesswinner indicate which number the snail bait wins and which number the player guesses. These two data are initialized here.
Action 2nd Frame
Code:
_ Root. wn1. _ x + = random (10)/10 + random (1); // Add a value to the x-axis data of snail 1
_ Root. wn2. _ x + = random (10)/10 + random (1); // Add a value to the x-axis data of snail 2
_ Root. wn3. _ x + = random (10)/10 + random (1); // Add a value to the x-axis data of snail 3
_ Root. wn4. _ x + = random (10)/10 + random (1); // Add a value to the x-axis data of snail 4
I have read the object article. Isn't it hard to understand it here?
_ X indicates one of the properties of the object snail bait: the position of the X axis.
+ = Indicates auto-increment. In this way, a = a + 1 is the same as a + = 1. Indicates auto-increment 1.
Here is a method, random ()
This method is used to obtain a random number, such as random (10), to obtain any number between 0 and 9. The preceding expression obtains a random number from 0 to 1.9.
Action 3rd frame
Code:
If (_ root. wn1. _ x> 540 ){
_ Root. truewinner = 1;
}
If (_ root. wn2. _ x> 540 ){
_ Root. truewinner = 2;
}
If (_ root. wn3._x> 540 ){
_ Root. truewinner = 3;
}
If (_ root. wn4. _ x> 540 ){
_ Root. truewinner = 4;
}
// Determine which snail bot arrives and change the value of truewinner when it arrives, recording which snail bot wins
If (_ root. truewinner! = 0 ){
If (_ root. guesswinner ==_root. truewinner ){
_ Root. gotoandstop (4 );
} Else {
_ Root. gotoandstop (5 );
}
} Else {
_ Root. gotoandplay (2 );
}
/*--------------------------------------------
Determine the value of truewinner. If it is changed, it indicates that there is already a snail bait.
If it hasn't changed, it means the snails haven't arrived yet.
If it doesn't arrive, jump the _ root pointer to 2nd frames and let them continue running. if it arrives,
The only difference between determining the pressure on a player is that it is not the same. If you guessed it, set the time
Pointer jump to 4th frames; otherwise, the jump to 5th frames.
---------------------------------------------*/
It is not difficult to understand the process carefully.
Next is the as on the button:
1st frame button
Code:
On (release ){
_ Root. Play ();
_ Root. guesswinner = 1;
}
The as on the button next to the first snail bait. After the button is released, the pointer jumps to 2nd frames and the variable guesswinner is recorded as 1 (guess 1st only ).
The code next to other snails is similar, that is, the record variable guesswinner value is the corresponding 2, 3, 4
The last button for replay.
Code:
On (release ){
_ Root. gotoandstop (1 );
_ Root. wn1. _ x = 25; // return to the starting point of snail 1
_ Root. wn2. _ x = 25;
_ Root. wn3. _ x = 25;
_ Root. wn4. _ x = 25;
}
Have you understood all of them? Well, look at what I did.