# problam: collision between 2 objects as3

**URL:** <https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798>\
**Category:** flash\
**Created:** [June 18, 2016, 8:11am UTC](https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798 "2016-06-18T08:11:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzvika\_Frenkel](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@Tzvika\_Frenkel](https://forum.kirupa.com/u/Tzvika_Frenkel)\
**Post date:** [June 18, 2016, 8:11am UTC](https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798/1 "2016-06-18T08:11:05Z")

</div>

Hi,  
i’m trying hard to make my MovieClip disappear when it hits another object.  
my problem is to understand how to manage the hitTest between the classes. Please help 🙂

for now, he says that “myObstacle” is not defined.

here is the document class:

package {

```
import flash.display.MovieClip;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;
import flash.events.Event;	

public class Main extends MovieClip {
	
	public var myGoodGuy:goodGuy = new goodGuy();
	public var myObstacle:obstacle = new obstacle();
	
	public function Main() {
		// constructor code
		createListeners();
		createObjectsOnStage();			
	}
	
	private function createObjectsOnStage():void {
		myGoodGuy.x = 20;
		myGoodGuy.y = 200;
		addChild(myGoodGuy);
		
		myObstacle.x = 200;
		myObstacle.y = 200;
		addChild(myObstacle);
	}
	
	private function createListeners():void {
		myGoodGuy.addEventListener(Event.ENTER_FRAME, MoveGoodGuy);
	}
	
	public function MoveGoodGuy(e:Event):void {			
		myGoodGuy.moveMe();
	}
}	

```

}

and this is the class of the moving object:

package {

```
import flash.display.MovieClip;
import flash.events.Event;		

public class goodGuy extends MovieClip {		
	
	public function goodGuy() {
		// constructor code
		stop();
		this.buttonMode = true;
		trace(obstacle);
	}
	
	public function changeState():void {
		this.gotoAndStop(Math.ceil(Math.random()*3));
	}
	
	public function moveMe(e:Event = null):void {
		this.x += 1;
		collision();
	}
	
	public function collision():void {
		if (this.hitTestObject(myObstacle)) {
			removeChild(myObstacle);
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![javadsavary](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@javadsavary](https://forum.kirupa.com/u/javadsavary)\
**Post date:** [June 19, 2016, 7:24am UTC](https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798/2 "2016-06-19T07:24:33Z")

</div>

[http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/](http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/)

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2016, 10:53pm UTC](https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798/3 "2016-06-19T22:53:50Z")

</div>

myObstacle is defined in Main. Assuming Main is the document class, you may be able to reference myObstacle from within goodGuy using MovieClip(root).myObstacle.

---

<div class="post-metadata">

**Author:** ![Tzvika\_Frenkel](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@Tzvika\_Frenkel](https://forum.kirupa.com/u/Tzvika_Frenkel)\
**Post date:** [July 2, 2016, 7:52am UTC](https://forum.kirupa.com/t/problam-collision-between-2-objects-as3/634798/4 "2016-07-02T07:52:00Z")

</div>

thank you! 🙂  
can you please show me how exactly to use the code you wrote inside the example?
