Tutorial
Racing games we always meet! Let me give you a brief explanation today.
Test the effect first:
Click here to download the source file
First create a new racing video clip, consisting of 6 layers of a basic racing car. When you put it on the stage, the name instance is named Car1.
Then make a car shadow movie clip based on the car above. When you drop it on the stage, the named instance name is Shadow1.
Then create a new empty movie clip. Name the instance name stepper when dragged onto the stage.
Then select the empty movie clip to enter the following code:
Onclipevent (load) {
speed1 = 0;
}
Onclipevent (enterframe) {
_root.step (1);
}
Then create a new layer action and enter the following code:
function Step (WHO) {
Check to the if the car in question is controlled by the player or by the computer
if (_root["car" +who].code = = "Player") {
We'll constantly decrease speed by multiplying it with a number below 1
if (this["Speed" +who]>0.3) {
this["Speed" +who] *= _root.speeddecay;
} else {
this["Speed" +who] = 0;
}
The car'll react to certain keys
Accelerate
if (Key.isdown (key.up) && this["Speed" +who]<_root.maxspeed) {
this["Speed" +who] + + = _root.acceleration;
}
Brake (reverse)
if (Key.isdown (Key.down)) {
this["Speed" +who]-= _root.backspeed;
}
Steer left
if (Key.isdown (key.left) && this["Speed" +who]>0.3) {
_root["Car" +who]._rotation-= _root.rotationstep* (this["Speed" +who]/_root.maxspeed);
}
Steer Right
if (Key.isdown (key.right) && this["Speed" +who]>0.3) {
_root["Car" +who]._rotation + + = _root.rotationstep* (this["Speed" +who]/_root.maxspeed);
}
this["Rotation" +who] = _root["Car" +who]._rotation;
We calculate the two components of speed (X axis and Y axis)
this["Speedx" +who] = Math.sin (this["Rotation" +who]* (math.pi/180)) *this["Speed" +who];
this["Speedy" +who] = Math.Cos (this["Rotation" +who]* (math.pi/180)) *this["Speed" +who]*-1;
Apply the components on the actual position of the car
_root["Car" +who]._x + + + this["Speedx" +who];
_root["Car" +who]._y + + + this["speedy" +who];
Position the shadow of the car
_root["Shadow" +who]._x = _root["Car" +who]._x-4;
_root["Shadow" +who]._y = _root["Car" +who]._y+2;
_root["Shadow" +who]._rotation = _root["Car" +who]._rotation;
}
if (_root["car" +who].code = = "Computer") {
}
}
Then create a layer and enter the following code (for initializing the variable)
Car1.code = "Player";
Acceleration = 0.4;
Speeddecay = 0.96;
Rotationstep = 10;
Maxspeed = 10;
Backspeed = 1;
Test it! It's getting off work today!! Tomorrow, we will make this game more complete! There are two steps behind it, you will find that the car has no site restrictions by testing the above animation! Then we'll limit it to a certain route. Expect!