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