Bandit
August 4, 2004, 7:14pm
1
Hello,

I am new to flash and actionscript, and I cannot find code for hit test anywhere, expecially for what I want it to do.

I have a movie clip of a car, and a movie clip of a tree. When the car hits the tree, I want the car movieclip’s speed to decrease to zero, making it stop. Can someone help me?

system
August 4, 2004, 7:23pm
2
if (car.hitTest(_root.tree)){
speed=0;
}

the middle is variable to you.

system
August 4, 2004, 7:28pm
3
Do I put that on the car or tree?

Here is the total code for the car.

```
onClipEvent(load) {
speed = 0;
}
onClipEvent(enterFrame) {
if (Key.isDown(Key.UP)) {speed += 1;
moving=1}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (speed>10) {
speed = 10;
}
if (speed<-10) {
speed = -10;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 1*(Math.abs(speed));
//Makes sure you can only turn if you're moving
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 1*(Math.abs(speed));
}
speed *= .9;
x = Math.sin(_rotation*Math.PI/180)*speed;
y = Math.cos(_rotation*Math.PI/180)*speed*-1;
_x = (_x+x);
_y = (_y+y);
}
if (car.hitTest(_root.tree)){
speed=0;
}
```

When I tested, I got this error:

Error Scene=Scene 1, layer=Layer 1, frame=1:Line 33: Statement must appear within on/onClipEvent handler
if (car.hitTest(_root.tree)){

Total ActionScript Errors: 1 Reported Errors: 1

Anyways, here is the .fla

And remember, I am new to this. please don’t make fun of my drawing.

http://venus.walagata.com/w/kwikemart614/carwontwork.fla

system
August 4, 2004, 7:40pm
4
```
onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 1;
moving = 1;
}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (speed>10) {
speed = 10;
}
if (speed<-10) {
speed = -10;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 1*(Math.abs(speed));
//Makes sure you can only turn if you're moving
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 1*(Math.abs(speed));
}
speed *= .9;
x = Math.sin(_rotation*Math.PI/180)*speed;
y = Math.cos(_rotation*Math.PI/180)*speed*-1;
_x = (_x+x);
_y = (_y+y);
if (this.hitTest(_root.tree)) {
speed = 0;
}
}
```

that on the car works.

system
August 4, 2004, 7:46pm
5
Thanks… It almost works. The only problem is that when I hit the tree, it doesn’t fully stop… It just goes slower.

http://venus.walagata.com/w/kwikemart614/carwontwork.swf

Also, the stage is 400x400. How do I make the car stop when it hits the border of the stage.

system
August 4, 2004, 7:52pm
6
```
onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 1;
moving = 1;
}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (speed>10) {
speed = 10;
}
if (speed<-10) {
speed = -10;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 1*(Math.abs(speed));
//Makes sure you can only turn if you're moving
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 1*(Math.abs(speed));
}
if (this.hitTest(_root.tree)) {
speed = 0;
speed *= 0;
}
speed *= .9;
x = Math.sin(_rotation*Math.PI/180)*speed;
y = Math.cos(_rotation*Math.PI/180)*speed*-1;
_x = (_x+x);
_y = (_y+y);
}
```

that’d be it.

system
August 4, 2004, 7:52pm
7
gimme a second on the border.

system
August 4, 2004, 7:57pm
8
Ok… Sorry to bother you again, but on that second code… When the car actually stops, the cars gets stuck in the tree. How can I make it be able to go backwards?

Once again, Im sorry for the inconvienence. hopefully I can do it myself next time.

http://venus.walagata.com/w/kwikemart614/carwontwork.swf

system
August 4, 2004, 8:31pm
9
Well, I have to go for today. Try to work on that… And thanks for the help.