Skip to main content
March 3, 2011
Answered

hitTestObject undefined property

  • March 3, 2011
  • 1 reply
  • 1150 views

Hello. i am making a game, it involes shooting targets. the way it works is i have a small dot (one pixel) and, when a mouse button is clicked, checks to see if its hitting a target.

in my target.as file i have the following error:

Line 23    1120: Access of undefined property poi.

poi is the instance name of thing that checks to see if its hitting the target. (point of impact)


if (target.hitTestObject(poi))

thats the code giving the error.

        public function Target()
        {
           
            this.addEventListener(MouseEvent.CLICK, detectCollision);
            //on mouseclick see if POI (point of impact) is over the target
            function detectCollision()
            {
                if (target.hitTestObject(poi))
                {
                    removeChild(target);
                    //remove the target, tell kongregate a target was hit
                    kongregate.stats.submit("TargetHit",1);
                    //there is no target onstage
                    var targetAlive = 0;
                }
            }


thats the code surrounding it.
thanks for helping

This topic has been closed for replies.
Correct answer moccamaximum

if "poi" is really the instancename of the object you should use:

target.hitTestObject(getChildByName("poi"))

also its possibly no good idea to use "target" or "Target" as class and instances, since it`s a reserved keyword in Actionscript

1 reply

CenturyMan1979
Inspiring
March 3, 2011

The error is saying it does not know what poi is so you need to set a value for poi before you check to see if target is hitting it. If poi is a sprite/movie clip then hitTestObject should work fine once you have poi set correctly. But if poi is a point then you might want to check out htiTestPoint as this might work better for you situation. You can check out hitTestPoint in the help docs at http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#hitTestPoint() . Also right above that is the htiTestObject which has an example so you can better understand how to implement it.

March 3, 2011

oh, srry. it is a movieClip, and i tried the hitTestPoint, it gives another error code, plus the same one.

"poi" is a small, one pixel by one pixel, movie clip that follows the mouse cursor. its only goes a certain speed, and thats why im not using the mouse.

how would i make the target.as recognize that it IS on the stage?

thanks again

CenturyMan1979
Inspiring
March 3, 2011

Is the the above code you provided all the code you have? If there is more it might be usefull to post the rest as this might help to determine the cause of the error. Also looking at your code again why do you have your detectCollision() function inside of your Target() function? Also your detectCollision() function is missing a parameter for the event that will be passed to it when the eventListener calls it. You will want to change it to detectCollision(evt:MouseEvent).