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

visual studio vs filezilla for publishing website

javascript jquery asp.net visual-studio-2010 filezilla

My first Question:Which is better publish a web site from visual studio,or upload it by filezilla?

move item to the front of the LinkedList

java linked-list nodes

my method is first to find certain element in the list, if true, the node contains the value is moved to the front of the list, without creating or deleting new nodes.here is what i have so far, i ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.