# Hit test with boundaries

I hope there is someone on here that can help me with this problem.

I am making a little test car game in which the player controls the car with key presses.I have got the actionscript for the car controls and that works fine,the problem i am having with this project is the boundary actionscript.

The actionscript i have for the car is :-

onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 1;
}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (Math.abs(speed)>20) {
speed *= .7;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}

``````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;
}
``````

The boundary is basically the work space on flash mx (600 by 400),what sort of code should i use? I have tried everything that i could think of and cant get it to work… =[

If you can help then thats great and thanks in advance…

There are various ways of doing it. Try this example I did. I use the hitTest so that the car only moves as long as its on the road, other wise bounces off the edges.

You could use a hit test to determine, that if you are not on the road, your speed, decreases, you sustain damage etc. Or you can use hitTest() to obviously see if you are hitting the walls, or trees.

For me, the example I posted works better, but it depends on the scenario of your game.

Also check out this 3d racing fla posted by someone on flashkit

I know I shouldn’t keep posting (hehe) but I just wanted to plug my site, and say that it’s mainly a games site, where I am posting my old fla of games I did, and maybe modify them and do some new ones. Check it out from time to time, because I plan to put a variety of fla’s up for people to down load - and the example I gave you was just a test, but a full racing game is in the pipeline…