1
votes

So basically I wanted a boy to eat up some mushrooms that was generated using addChild. and i made a hittest for it. But i had an error 1120 of undefined property. how can I slove this? any help would be much appreciated.

heres my code.

    var timer:Timer = new Timer(1000,10);
timer.addEventListener(TimerEvent.TIMER, addTarget);
timer.start();

var score:int = 0;

function addTarget(e:TimerEvent)
{
    var posX = Math.random()*860;
    var posY = Math.random()*500;


    var mushroom:Mushroom = new Mushroom();
    addChild(mushroom);


    mushroom.x = posX;
    mushroom.y = posY;
    boy_mc.addEventListener(Event.ENTER_FRAME, scoring);
}


function scoring(e:Event)
{
    trace("test");
    if (boy_mc.hitTestObject(mushroom))

    {
    score = score + (MovieClip(e.currentTarget).point);

    score_txt.text = String(score);
    }
}

----------------and heres my class file----------

package  
{
    import flash.display.MovieClip;

    public class Mushroom extends MovieClip
    {
        private var size:Number;


        public var point:int;


        public var mushroom:int;

        public function Mushroom() 
        {


            // constructor code
            size = (Math.random()*100)+20;
            this.width = size;
            this.height = size;

            point = Math.random()*10;
        }

    }

}
1
error :is on this " if (boy_mc.hitTestObject(mushroom))"Access of undefined property mushroom.H4dies
For completion, you should add the error message to the question itself by editing itbrodoll

1 Answers

0
votes

You need of the mushrooms reference, like a Array() or Vector.<Mushroom>() of that. Then, access the reference in for.

var mushrooms:Vector.<Mushrooms> = new Vector.<Mushrooms>();

var timer:Timer = new Timer(1000,10);
timer.addEventListener(TimerEvent.TIMER, addTarget);
timer.start();

var score:int = 0;

function addTarget(e:TimerEvent) {

    var posX = Math.random()*860;
    var posY = Math.random()*500;

    var mushroom:Mushroom = new Mushroom();
    mushrooms.push(mushroom); //add the new mushroom in vector
    addChild(mushroom);

    mushroom.x = posX;
    mushroom.y = posY;
    boy_mc.addEventListener(Event.ENTER_FRAME, scoring);

}


function scoring(e:Event) {

    var totalMushrooms:int = mushrooms.length;

    for(var i:int = 0; i < totalMushrooms; i++) {
        if (boy_mc.hitTestObject(mushrooms[i])) { //mushroom reference
            score = score + (MovieClip(e.currentTarget).point);
            score_txt.text = String(score);
            removeChild(mushrooms[i]); //remove
            mushrooms.splice(i, 1); //remove mushroom from vector
        }   
    }

}