problam: collision between 2 objects as3

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 :slight_smile:

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

http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

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.

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