开发者

hitTestObject (AS3) Error: Access of undefined property

开发者 https://www.devze.com 2023-01-17 18:13 出处:网络
In the function hitWall, hitTestObject is working when a player hits the wall. I have walls on the flash stage, named wall, wall2, wall3, wall4. Using that same concept, I placed other movieclips (tre

In the function hitWall, hitTestObject is working when a player hits the wall. I have walls on the flash stage, named wall, wall2, wall3, wall4. Using that same concept, I placed other movieclips (trees) and named them: tree1, tree2, etc....etc...

I'm receiving an error message: Access of undefined property tree.

wall.addEventListener(Event.ENTER_FRAME, hitWall);
tree.addEventListener(Event.ENTER_FRAME, hitTree);

// function hitWall --------------------------------------------------------------
    function hitWall(event:Event):void {
        if (player_ary[me].开发者_StackOverflowhitTestObject(wall)) {
                player_ary[me].y+=6;
            } else if (player_ary[me].hitTestObject(wall2)) {
                player_ary[me].y-=6;

            } else if (player_ary[me].hitTestObject(wall3)) { 
                player_ary[me].x+=6;

            } else if (player_ary[me].hitTestObject(wall4)) {
                player_ary[me].x-=6;
            }

    }

    // function hitWall --------------------------------------------------------------
    function hitTree(event:Event):void {
        if (player_ary[me].hitTestObject(tree)) {
                player_ary[me].y+=6;
            } else if (player_ary[me].hitTestObject(tree2)) {
                player_ary[me].y-=6;
            } else if (player_ary[me].hitTestObject(tree3)) { 
                player_ary[me].x+=6;
                    }
     }


The error indicates that the tree MovieClip is null, so the problem is not in this part of your code.

If the tree MovieClip is meant to be an instance of a tree added to the stage, make sure that the instance exists before calling a method on it.


if the issue is what I think it is...

the problem is that when you have a class or a function defined like this...

{
   var wall:Object = {name:"dodo"};
   wall.addEventListener(Event.ENTER_FRAME,doThis)
   function doThis(event:Event):void{
      trace (wall.name);
   }
}

wall is actually a null. the whole function is called outside of the context of the function you define it in.

what you will need to do is either test for e.target (which will return the wall object, because that's where you added the event listener) or add another event listener, and use a class variable (ie. public var a:int)

this is a common issue with people switching from as2 to as3

0

精彩评论

暂无评论...
验证码 换一张
取 消