Search code examples
actionscript-3addchild

Action Script 3: Addchild within an already added child issue


My issue is that i get an error when my function addSpotlight is called.

TypeError: Error #1010: A term is undefined and has no properties.
    at BubbleBoy_fla::MainTimeline/addSpotlight()
    at BubbleBoy_fla::MainTimeline/policeHeli()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

My code is this:

//Adding Helicopter
function addHelicopter(xLocation: int, yLocation: int, passSpeedX, passSpeedY): void {
    var helicopter: Helicopter = new Helicopter(xLocation, yLocation, passSpeedX, passSpeedY);
    back.addChild(helicopter);
    heliList.push(helicopter);
    playFly();
}

//Adding Spotlight
function addSpotlight(xSpotlightLocation: int, ySpotlightLocation: int, spotlightSpeedX): void {
    var spotlight: Spotlight = new Spotlight(xSpotlightLocation, ySpotlightLocation, spotlightSpeedX);
    back.helicopter.addChild(spotlight);
    spotlightList.push(spotlight);
}

The reason i am doing this is because the helicopter can be destroyed and the spotlight gets removed at the same time, but if you don't destroy the helicopter and the spotlight hits you then you are caught.

I previously had my code just add the spotlight to the back container then i lined it up with the helicopter, but then this would only remove the first spotlight in the array.

if (heliList.length > 0) {
    for (var h: int = 0; h < heliList.length; h++)
        for (var l: int = 0; l < spotlightList.length; l++) {
            if (heliList[h].hitTestPoint(boy.x + upBumpPoint.x, boy.y + upBumpPoint.y, true)) {
                trace("SCORE POINTS")
                score += 100;
                playCrash();
                playFly();
                heliList[h].removeHeli();
                spotlightList[l].removeSpotlight();

                //policeTimer.start();
            }
        }
}

Any suggestions on these 2 issues??


Solution

  • In addSpotlight use:

    trace('spotlight: ', spotlight);
    trace('back: ', back);
    trace('back.helicopter: ', back.helicopter);
    trace('spotlightList: ', spotlightList);
    

    Whatever is missing - it causes the issue :)