Learn flash, do not learn ActionScript, the things you do is limited. But many friends are complaining that as is too difficult to learn. I want to tell you, in fact, as it is not difficult to learn. Now, I'm going to teach you to make a tank that can be started at will and rotate its fort (it feels like playing a remote car, very interesting). In this article, we will learn some basic statements, properties, objects, and methods in as.
OK, let's go to study here. First, create a new file, 600*450 pixels, the background is black, 40fps.
One, to achieve turret rotation
1. Press the shortcut key Ctrl+f8 to create a movie clip symbol named turret. Then draw a turret in its editing scene, as shown in Figure 1. Note that the turret rotates around the "╬" word in the center of the scene. So everyone in the production time to the turret round the center of the cover and component scenes in the "╬" word overlap.
2. Press the shortcut key Ctrl+f8 to create a movie clip symbol called "tank." Next, draw the body of a tank in "Layer 1" of its edit scene. Then drag the turret component from the library to layer 2, as shown in Figure 2.
3. Click the turret component in Layer 2, press F9 to open the Actions panel, and enter the following code:
Perform when a key is pressed on the keyboard
Onclipevent (KeyDown) {
If you press the "Z" key, the turret rotates 6 degrees to the left.
if (key.getcode () = = 90) {
_rotation-= 6;
}
If you press the "X" key, the turret rotates 6 degrees to the right.
if (key.getcode () = = 88) {
_rotation + 6;
}
}
Test, you will find that the turret can now rotate. Because we change the angle of "turret" by _rotation properties. Key.getcode () returns the keyed code value of the last key pressed. However, it is also possible for readers to use other keys to control the turret selection. What key to use, we can check the code of the key--the letter-A-Z corresponding to the number is 65-90. No matter what keys you use, pay attention to such a principle-user-friendly operation.
Second, the realization of tank movement
Back to Scenario 1, open the library, drag the tank component into the scene, click on the "Tank" component, press F9 to open the Actions panel, and enter the following code:
Onclipevent (enterframe) {
The move function realizes the movement of the tank by changing the coordinate value of the tank
function Move () {
_y-= (Math.Cos (math.pi/180*this._rotation)) *speed;
_x + = (Math.sin (math.pi/180*this._rotation)) *speed;
}
If you tap the "↑" key or the "↓" key on the keyboard, call the Move function
if (Key.isdown (key.up)) {
Move ();
}
if (Key.isdown (Key.down)) {
Move ();
}
If you hit the "←" key or "→" button on the keyboard, let the tank rotate
if (Key.isdown (Key.left)) {
_rotation-= 3;
}
if (Key.isdown (key.right)) {
_rotation + 3;
}
Change the velocity offset of the tank to make the tank move with an acceleration
if (Key.isdown (key.up) &&! ( Key.isdown (Key.down)) && speed>-3 && speed<3) {
Speed + 0.1;
}
if (Key.isdown (Key.down) &&! ( Key.isdown (key.up)) && speed>-3 && speed<3) {
Speed-= 0.1;
}
if ((speed>0.1) && (! Key.isdown (Key.up)) && (! Key.isdown (Key.down))) {
Move ();
Speed-= 0.05;
}
if ((speed<-0.1) && (! Key.isdown (Key.up)) && (! Key.isdown (Key.down))) {
Move ();
Speed + 0.05;
}
If you hold down two opposing keys at the same time, the tank won't keep moving.
if ((Key.isdown (key.up)) && (Key.isdown (Key.down)) {
Speed/= 1.1;
if ((speed>-0.1) && (speed<0.1)) {
Speed = 0;
}
}
Keep the tank moving in the scene all the Times
if (_x<=0) {
_x = 600;
else if (_x>=600) {
_x = 0;
}
if (_y<=0) {
_y = 450;
else if (_y>=450) {
_y = 0;
}
}
A large section of code above, the reader may not be able to digest. Now I'm going to talk about the code carefully.
1. As you can see, the move function is used in many parts of the program, and if we use the code directly from the moves function, it will make the entire program verbose. Moreover, this is not convenient for the maintenance of the program.
2. When the object is in motion, there is always a changing acceleration. When the tank is moving in the direction of change, the changing angle is constantly changing. By code:
_y-= (Math.Cos (math.pi/180*this._rotation)) *speed;
_x + = (Math.sin (math.pi/180*this._rotation)) *speed;
achieve this change. Where the y axis coordinates with "=" is related to the flash coordinate set. Because, in the home scene, moving up the element, you need to reduce the Y value.
The 3.key.isdown () method returns a true value when the key specified in "()" is pressed. We usually use this method to detect whether to press the SHIFT key and the TAB key.
Now test it, you will find it very interesting.