if (_root["car"+who].code == "player") {
//we will constantly decrease speed by multiplying it with a number below 1
//trace(this);
if (this["speed"+who]>0.3) {
this["speed"+who] *= _root.speedDecay;
} else {
this["speed"+who] = 0;
}
//---------------------------------------------------------------
//the car will react to certain keys
//accelerate
if (Key.isDown(Key.UP) && this["speed"+who]<_root.maxSpeed) {
//if the acceleration key is down, the engine sound volume is set to 100
this["speed"+who] += _root.acceleration;
}
//brake (reverse)
if (Key.isDown(Key.DOWN)) {
this["speed"+who] -= _root.backSpeed;
}
//steer left
if (Key.isDown(Key.LEFT) && Math.abs(this["speed"+who])>0.3) {
_root["car"+who]._rotation -= _root.rotationStep*(this["speed"+who]/_root.maxSpeed)/1.25
//_root["car"+who].drehen(-_root.rotationStep*(this["speed"+who]/_root.maxSpeed), 8);
//drehle = _root["car"+who]._rotation -= _root.rotationStep*(this["speed"+who]/_root.maxSpeed);
//_root["car"+who].drehen(drehle, 10);
}
//steer right
if (Key.isDown(Key.RIGHT) && Math.abs(this["speed"+who])>0.3) {
_root["car"+who]._rotation += _root.rotationStep*(this["speed"+who]/_root.maxSpeed)/1.25
}
//---------------------------------------------------------------
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];
//the collisions
//define the four collision points
_root["car"+who].pointLeft = {x:-20, y:0};
_root["car"+who].localToGlobal(_root["car"+who].pointLeft);
_root["car"+who].pointRight = {x:20, y:0};
_root["car"+who].localToGlobal(_root["car"+who].pointRight);
_root["car"+who].pointFront = {x:0, y:-25};
_root["car"+who].localToGlobal(_root["car"+who].pointFront);
_root["car"+who].pointBack = {x:0, y:25};
_root["car"+who].localToGlobal(_root["car"+who].pointBack);
//let's use some shorter variable names :)
this["lpx"+who] = _root["car"+who].pointLeft.x;
this["lpy"+who] = _root["car"+who].pointLeft.y;
this["rpx"+who] = _root["car"+who].pointRight.x;
this["rpy"+who] = _root["car"+who].pointRight.y;
this["fpx"+who] = _root["car"+who].pointFront.x;
this["fpy"+who] = _root["car"+who].pointFront.y;
this["bpx"+who] = _root["car"+who].pointBack.x;
this["bpy"+who] = _root["car"+who].pointBack.y;
//check for collisions
if (_root.terrain.hitTest(this["lpx"+who], this["lpy"+who], true)) {
_root["car"+who]._rotation += 5;
this["speed"+who] *= 0.85;
}
if (_root.terrain.hitTest(this["rpx"+who], this["rpy"+who], true)) {
_root["car"+who]._rotation -= 5;
this["speed"+who] *= 0.85;
}
if (_root.terrain.hitTest(this["fpx"+who], this["fpy"+who], true)) {
this["speed"+who] = -1;
}
if (_root.terrain.hitTest(this["bpx"+who], this["bpy"+who], true)) {
this["speed"+who] = 1;
}