hitTestObject in Actionscript 3 error 1046

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.

Answers


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.

if(wall.hitTestObject(Player))
{
   trace("player hit");
 }

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/


Where have you defined Player?

The error shows that the compiler doesn't know what Player is.. Is it its own class? If so, this hitTestObject test won't work -- you need to compare two objects, not one object and one class.

This can be fixed by declaring an instance of the Player class (if you have one), maybe in the Twinrunner.as file:

public static var player:Player = new Player();
player.x = whatever;
player.y = whatever;
addChild(player);

Then, to do the hitTest:

if(this.hitTestObject(twinrunner.player))
        {
            trace("player hit");
        }

And if you don't actually have a Player class, then you'll need to make one.

Or, is Player actually on the Stage? If so, you'll need to change things up a bit. Comment and let me know if this is the case.


Need Your Help

Errorcode in MySQL procedure

mysql procedure mysql-error-1146 getlasterror

We have been facing a small problem in MySQL procedure. We have placed some exception handlers inside the procedure. But we want to retrieve the errorcode of any error that can occur inside a proce...

How to prevent a form to be sent when hitting enter on a none-form input-field? (DNN)

javascript jquery dotnetnuke-5

On a page I have a google search-field and a separate form for a login. In order to make the search-field work with enter, I included the following script:

PHP array in object converting to string (so far as I can tell)

php debugging

I'm developing front-end code for a web application, and ran into an odd piece of trouble with a custom object. When I request the object and use print_r() I get this (the object is a lot bigger; ...