Question

I am trying to access a child so I can do some hitTest with that child but I get this error, which you may have seen a million times, and enough times to understand what it means, unlike me (new to As3).

1119: Access of possibly undefined property ground through a reference with static type flash.display:Sprite.

Saything, here is some code.

I initiate everything in the main constructor and then add it to the stage

        addChild(_scrollLayer);
        //player
        _scrollLayer.addChild(character);
        //character.reset();
        //character.x = 640;
        character.y = 0;

        //level
        _scrollLayer.addChild(ground);
        ground.x = 640;
        ground.y = 680;


        //greenGoblin
        _scrollLayer.addChild(goblin1);
        goblin1.x = 500;
        goblin1.y = 0;

        _scrollLayer.addChild(goblin2);
        goblin2.x = 800;
        goblin2.y = 0;

        _scrollLayer.addChild(goblin3);
        goblin3.x = 100;
        goblin3.y = 0;

        //red Goblin
        _scrollLayer.addChild(redGoblin1);
        redGoblin1.x = 1100;
        redGoblin1.y = 400;



        while (_scrollLayer.ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true))

This is when I get the ERROR message that you have seen above.

I have tried doing this also

        while (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true))

No error message, how ever the hitTest does not work, this is because everything is in a container.

Here is the full hit Test code

        for (var c:int = 0; c < childrenOnStage; c++)
        {
            if (getChildAt(c).name == "player")
            {       
                if (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true))
                {
                    touchingGround = true;
                    while (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true))
                    {

                        OnGround(getChildAt(c)).incrementUp();

                        if (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true))
                        {

                        }
                        else
                        {
                            OnGround(getChildAt(c)).keepOnGround();
                        }
                    }
                }
                else
                {
                    touchingGround = false;
                    //trace("not touch");
                    //character.gotoAndStop("jump");
                }
            }

//childrenOnStage is a variable. childrenOnStage = this.numChildren;

I do not understand why the hit test is not working. I would appreciate an answer, advice or tip please.

Thank you.

Was it helpful?

Solution

I may be a little rusty at hit testing in this manner (I use the visual IDE more heavily than most,) but I think I remember this problem.

If I remember right, DisplayObject objects are not declared the same as programming objects, in that you cannot simply call their name to access them. AS3 thinks you're calling an attribute on the Sprite. (That said, if you were working from the timeline, that'd be another story.)

The following code SHOULD work, though is it untested. Your code isn't seeing 'ground' as a DisplayObject, so it can't call "hitTestPoint" on it. Thus, you have to get around that.

var mcGround:MovieClip = MovieClip(_scrollLayer.getChildByName("ground"));
mcGround.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true);

(Thanks to Amarghosh's answer here - I cross-checked my code with it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top