# Hit test help

**URL:** <https://forum.kirupa.com/t/hit-test-help/59912>\
**Category:** flash\
**Created:** [August 4, 2004, 7:14pm UTC](https://forum.kirupa.com/t/hit-test-help/59912 "2004-08-04T19:14:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bandit](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Bandit](https://forum.kirupa.com/u/Bandit)\
**Post date:** [August 4, 2004, 7:14pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/1 "2004-08-04T19:14:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:23pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/2 "2004-08-04T19:23:47Z")

</div>

if (car.hitTest(\_root.tree)){  
speed=0;  
}

the middle is variable to you.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:28pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/3 "2004-08-04T19:28:47Z")

</div>

Do I put that on the car or tree?

Here is the total code for the car.

```auto
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](http://venus.walagata.com/w/kwikemart614/carwontwork.fla)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:40pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/4 "2004-08-04T19:40:18Z")

</div>

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:46pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/5 "2004-08-04T19:46:51Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:52pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/6 "2004-08-04T19:52:06Z")

</div>

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:52pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/7 "2004-08-04T19:52:38Z")

</div>

gimme a second on the border.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 7:57pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/8 "2004-08-04T19:57:50Z")

</div>

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](http://venus.walagata.com/w/kwikemart614/carwontwork.swf)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 4, 2004, 8:31pm UTC](https://forum.kirupa.com/t/hit-test-help/59912/9 "2004-08-04T20:31:48Z")

</div>

Well, I have to go for today. Try to work on that… And thanks for the help.
