A Flash Developer Resource Site

# Thread: car game hit a building

1. ## I`m making a car game. You can drive the car around, but I dont know how I can make the car bounce back when it hit a building or something like that. I know that i have to use some kind of hit.test, but not exactly how. Im shure it`s some flash dudes out there that can help me! Right?!  Reply With Quote

2. If your car is moving on the x axis and if you use a speed variable (eg : car._x = car._x + xspeed), you can paste and modify this piece of code on the instance of the wall movie. Basically when it detects the collision between car and wall it inverses the speed of the car and lower it, and then moves the car to the opposite direction so that the car leaves the hit area.

onClipEvent (enterFrame) {
if (this.hitTest(car._x, car._y, false) == true) {
car.xspeed = -car.xspeed * 0.66;
car._x = car._x-car.xspeed;
}
}  Reply With Quote

3. ## I`t did`nt work...

I tried that but it did`nt work. I make an invisible MC and in the MC i give it these actions:

movieclip.prototype.drehung = function() {
this._y -= (Math.cos(Math.PI/180*this._rotation))*w;
this._x += (Math.sin(Math.PI/180*this._rotation))*w;
};
movieclip.prototype.carcontrol = function(faktorN, faktorP, fG, rG, BminX, BmaxX, BminY, BmaxY, Sfaktor, BnMinX, BnMaxX, BnMinY, BnMaxY) {
if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP)) {
this._rotation -= faktorN;
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) {
this._rotation += faktorP;
}
if (Key.isDown(Key.LEFT) && w>.1 && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN))) {
this._rotation -= faktorN;
}
if (Key.isDown(Key.RIGHT) && w>.1 && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN))) {
this._rotation += faktorP;
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN)) {
this._rotation += faktorP;
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN)) {
this._rotation -= faktorN;
}
if (Key.isDown(Key.UP)) {
this.drehung();
}
if (Key.isDown(Key.DOWN)) {
this.drehung();
}
if (Key.isDown(Key.UP) && !(Key.isDown(Key.DOWN)) && w>-rG && w<fG) {
w += .5;
}
if (Key.isDown(Key.DOWN) && !(Key.isDown(Key.UP)) && w>-rG && w<fG) {
w -= .5;
}
if ((w>.1) && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN))) {
this.drehung();
w -= .5;
}
if ((w<-0.1) && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN))) {
this.drehung();
w += .5;
}
if ((w<.1) && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN)) && w>=0) {
w = 0;
}
if ((w>-.1) && (!Key.isDown(Key.UP)) && (!Key.isDown(Key.DOWN)) && w<=0 || Key.isDown(Key.UP) && (Key.isDown(Key.DOWN))) {
w = 0;
}
this.Grenze(BminX, BmaxX, BminY, BmaxY, Sfaktor, BnMinX, BnMaxX, BnMinY, BnMaxY);
};
movieclip.prototype.Grenze = function(minX, maxX, minY, maxY, faktor, nMinX, nMaxX, nMinY, nMaxY) {
if (tempo>0) {
tempo = tempo-faktor;
}
if (this._x>=maxX) {
this._x = nMinX;
}
if (this._x<=minX) {
this._x = nMaxX;
}
if (this._y>=maxY) {
this._y = nMinY;
}
if (this._y<=minY) {
this._y = nMaxY;
}
};
this._visible = 0;
_parent.onEnterFrame = function() {
_parent[_targetInstanceName].carcontrol(leftTurn, rightTurn, forwardSpeed, backwardSpeed, BorderminX, BordermaxX, BorderminY, BordermaxY, Speedfactor, NewMinX, NewMaxX, NewMinY, NewMaxY);
};

Do you know how i can do it with these actions?  Reply With Quote

4. Can you show off what your game is looking like? It will be easier for me to understand your code.
Have you write it on your own ? You use prototype constructor wich I find somehow complex...  Reply With Quote

5. Driving Car Boundarys
This is a tutorial explaining how you would make a car based game using boundarys.

I will explain how this is done, step by step:

1.Create an outline of where you would like your boundarys to be, I used a square, Make this a MC and give it a instance name land.

2.Design a car, and turn that into a MC, No need to give it an instance name because all the actions will be in it.

3.Click on your car and bring up the actions panel, here are the actions to add:

onClipEvent (enterFrame) {
// make the car go forward
if (Key.isDown(Key.UP)) {
speed += 1;
}
// make the car go backwards
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
// tells the car to slow down after the speed of 20
if (Math.abs(speed)>20) {
speed *= .7;
}
// you can change the rotation of the car to your desire
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
// here is where the hittest is for the boundary
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}

4.Finished, now test it out.  Reply With Quote

6. ## It realy works! Thanks!

It works but i want it work on more than one object.
So i tried copy that hit.test thing.
It worked somehow, but when i crashed into the new object it just drived sloooooooooowly trought it.
I changed the Instance name and everything.  Reply With Quote

7. Here, name the boundry "land". Then copt the following code into the car:

onClipEvent (enterFrame) {
// make the car go forward
if (Key.isDown(Key.UP)) {
speed += .5;
}
// make the car go backwards
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
// tells the car to slow down after the speed of 20
if (Math.abs(speed)>6) {
speed *= .7;
}
// you can change the rotation of the car to your desire
if (Key.isDown(Key.LEFT)) {
_rotation -= 5;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 5;
}
// here is where the hittest is for the boundary
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
Then everyting should be fine... Did it work?  Reply With Quote

8. When i crash in a object now, it dosent bounce before it`s in the middle. How can you make it bounce when it`s on the edge of the car.  Reply With Quote

9. Originally posted by Bananas in pyjamas
When i crash in a object now, it dosent bounce before it`s in the middle. How can you make it bounce when it`s on the edge of the car.
Would very much like to know this also ...  Reply With Quote

10. ## There are 2 types of colision detection one uses a middle point (Witch you have) and the other makes a box around the object and detcts that so the car might bounce before it hits the object. those are your choices. I dont know how to do the colision detection with the box. I hope this helps.  Reply With Quote

11. The boundry box hitTest uses no shape flag, ie this.hitTest(_root.enemy).

What I have been trying to find out for AAAAAAAAAGES is how to do a hitTest that only detects a hit with the filled in area, the boundry method is useless for irregular shapes and the hitTest that detects a hit with the middle point is rather useless in my opinion.
Other games seem to do it so there must be a way! Must find out ...  Reply With Quote

12. I dont know either. I've been trying to figure out how to do this as well since I've started. Post a new thread?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 