I just began to do some programming in AS3 but there are still some things which aren’t clear to me.
I have project with 3 different files: twinrunner.fla, twinrunner.as and wall.as.
I let the the wall scroll over the screen from right to left. When it hits the player I want to let it recognize it. I tried to accomplish this with the hitTestObject. But unfortunately I only get errors.
twinrunner.as
public class TwinRunner extends MovieClip
{
private var _timer:Timer;
public function TwinRunner()
{
//Timer initialize
_timer = new Timer(500, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onUpdateTime);
_timer.start();
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
//Add event listeners
button.addEventListener(MouseEvent.CLICK, onGuessButtonClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
//Remove the onEnterFrame event if
//this object is removed from the stage
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onGuessButtonClick(event:Event):void
{
addChild(new Wall());
}
private function onEnterFrame(event:Event):void
{
}
private function onUpdateTime(event:Event):void
{
addChild(new Wall());
_timer.reset();
_timer.start();
}
}
wall.as
public class Wall extends MovieClip
{
private var _vx:int;
public function Wall()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
x = stage.width;
y = 300;
_vx = -5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage)
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
trace("wall removed");
}
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
_vx = -20;
}
}
private function onEnterFrame(event:Event):void
{
x += _vx;
if(this.hitTestObject(Player))
{
trace("player hit");
}
if (x + width / 2 < 0)
{
parent.removeChild(this);
}
}
}
Every time I try to compile this I get the error 1046:
type not found or not a compile-constant: Player
I guess it is a pretty stupid mistake but I am totally lost and don’t know where to search.
I don’t understand the concept of this game completly so I’ll give you a solution based on what I’ve already understand.
At first I think it’ll be better not to use this, but actually the instance name of the wall.
Also width is nowhere to be found, which means that you have to add (probably you forgot it).
var witdh:Number;
Also hitTestObject is not so precise method for collision. You can calculate the distance between the 2 objects and check if they collide.
This can help you:
http://sierakowski.eu/list-of-tips/39-collision-detection-methods-hittest-and-hittestobject-alternatives.html
http://ughzoid.wordpress.com/2011/06/20/collision-detection-alternatives-to-hittest-and-hittestobject/