For all of you people who wanted to make a gta game but didn't know the code to make the controls change when the person entered the car, I have started to work on a code for that exact situation. It is not even close to being finished but I have started working on it so I thought I might show you. First off, create the person you will control and convert it to a MC and give it the instance name of "person" without the quotes. Then give him this code:
onClipEvent(load){
speed=0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)&& _root.person.hitTest (_root.car)) {
speed += 1;
}
// This will make the car go backwards
if (Key.isDown(Key.DOWN)&& _root.person.hitTest (_root.car)) {
speed -= 1;
}
//The car will start to slow down after the speed of 25
if (Math.abs(speed)>25) {
speed *= .6;
}
// This will change the angle of the car
if (Key.isDown(Key.LEFT)&& _root.person.hitTest (_root.car)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)&&_root.person.hitTest (_root.car)) {
_rotation += 15;
}
// This will make the car move
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
on (press) {
startDrag ('');
}
on (release) {
stopDrag ();
}
Note: When I was working on this code, I didn't know how to make it so that if the person is not on the car, he would walk with normal controls so just to test the code, you can drag the person on to the car and then press the buttons to make the car move. Then you can drag the person off the car and press the same buttons and you will see that nothing will happen.
Then make the car MC and give it the instance name of "car" without the quotes. After that, give it these actions:
onClipEvent (load) {
speed=0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)&& _root.person.hitTest (_root.car)) {
speed += 1;
}
if (Key.isDown(Key.DOWN)&& _root.person.hitTest (_root.car)) {
speed -= 1;
}
//The car will start to slow down after the speed of 25
if (Math.abs(speed)>25) {
speed *= .6;
}
// This will change the angle of the car
if (Key.isDown(Key.LEFT)&& _root.person.hitTest (_root.car)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)&& _root.person.hitTest (_root.car)) {
_rotation += 15;
}
// This will make the car move
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
Another Note: Another thing I didn't complete was I didn't know how to make it so once the character is on the car, he could get off. So for as of now, you can only drag and drop the person on and off the car.
As you can see, this code is still VERY buggy, and if anyone would like to help me work out the problems I said in the notes, you can look up my contact info in my profile. And the car physics in the code aren't mine, I just used them to test my code and all. So please tell me how this code is in your replys!