Question

I have tried removing programatically added movieclips when they collide with a certain object. When they do, they vanish. But after they have vanished, I get a #1009 error.

The error points at line 95, which is

if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2))"

And sometimes at 59, which is

if (this.x >= stage.stageWidth - 20)"

Why does it keep trying to run line 95 (and sometimes) 59? I removed the eventlistener.

package
{
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.utils.*;
    public class Worker extends MovieClip
    {

        private var _root:MovieClip;
        private var actualRange:Number;
        private var reachedGoal:Boolean = false;
        private var b:uint;
        private var destination:Number;
        private var goRight:Boolean = true;
        private var goNowhere:Boolean = true;
        private var yDir:Number;
        private var yNumber:Number;

            public function Worker()
            {
                addEventListener(Event.ADDED, beginClass);
                addEventListener(Event.ENTER_FRAME, loop);
                //defining _root as the document root

                actualRange =(Math.floor(Math.random() * (520 - 80 + 1)) + 80);

                b = setInterval(startInc, 3000);
            }


        private function beginClass(event:Event):void{
            //defining _root as the document root
            _root = MovieClip(root);
        }

            private function loop(event:Event):void
            {
                if (this.hitTestObject(_root.killGun))
                {
                    {
                        removeEventListener(Event.ENTER_FRAME, loop);
                        _root.removeChild(this);
                    }
                }

                if (reachedGoal == true)
                {

                    if (goNowhere == true)
                        {
                            gotoAndStop(1);
                        }
//Boundaries
                    if (this.x <= 20)
                    {
                        goRight = true;
                        goNowhere = false;
                    }
                    if (this.x >= stage.stageWidth - 20)
                    {
                        goRight = false;
                        goNowhere = false;
                    }

                    if (this.y >= stage.stageHeight - 20)
                    {
                        yDir = 0
                    }

                    if (this.y <= 20)
                    {
                        yDir = 1
                    }
                }

//Intro walk
                if (reachedGoal == false)
                {
                    if (this.x < actualRange)
                        {
                            gotoAndStop(4);
                            this.x += 3
                        }   

                    if (this.x >= actualRange)
                        {
                            reachedGoal = true;
                            gotoAndStop(1);
                        }
                }


//Main walk
                if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2))
                {
                    if (goNowhere == false)
                    {
                            if (goRight == true)
                            {
                                this.x += 1;
                                gotoAndStop(2);

                                //Y value
                            }
                            if (goRight == false)
                            {
                                if (this.x < stage.stageWidth - this.width /2)
                                this.x -= 1
                                gotoAndStop(3);

                                //Y value

                                if (yNumber == 1)
                                {                                   
                                    if(yDir == 0)
                                    {
                                        this.y -= 1;
                                    }
                                    if (yDir == 1)
                                    {
                                        this.y += 1;
                                    }
                                }

                            }

                    }

                }


            }


            //Timer functions
            private function completePlay()
            {
                clearInterval(b);
            }

            private function startInc()
            {   
                    var moveNumber:Number = Math.floor(Math.random() * 20);

                    if (moveNumber == 1)
                    {
                        destination = Math.floor(Math.random() * 50);   
                        yDir = Math.floor(Math.random() * 2);
                        yNumber = Math.floor(Math.random() * 4);

                        goRight = true;
                        goNowhere = false;
                    }

                    else if (moveNumber == 2)
                    {   
                        destination = Math.floor(Math.random() * 50);       
                        yDir = Math.floor(Math.random() * 2);
                        yNumber = Math.floor(Math.random() * 4);

                        goRight = false;
                        goNowhere = false;
                        gotoAndStop(3);
                    }

                    else
                    {
                        goNowhere = true;
                    }

            }           


//End

    }

}
Was it helpful?

Solution

The error is happening because you're removing the eventListener within the eventListener's function and your removing the object from it's parent and still processing data that the object no longer contains or has been nullified. You should add a boolean to tell if the hitTest has occured and then not process any more of the loop. For instance:

private function loop(event:Event):void
    {
    var objectHit:Boolean = false;

    objectHit = this.hitTestObject(_root.killGun);

    if(!objectHit) {
        if (reachedGoal == true)
        {

            if (goNowhere == true)
            {
                gotoAndStop(1);
            }
            //Boundaries
            if (this.x <= 20)
            {
                goRight = true;
                goNowhere = false;
            }
            if (this.x >= stage.stageWidth - 20)
            {
                goRight = false;
                goNowhere = false;
            }

            if (this.y >= stage.stageHeight - 20)
            {
                yDir = 0
            }

            if (this.y <= 20)
            {
                yDir = 1
            }
        }

        //Intro walk
        if (reachedGoal == false)
        {
            if (this.x < actualRange)
                {
                gotoAndStop(4);
                this.x += 3
            }   

            if (this.x >= actualRange)
            {
                reachedGoal = true;
                gotoAndStop(1);
            }
        }
    }

    if (objetHit)
    {
        removeEventListener(Event.ENTER_FRAME, loop);
        _root.removeChild(this);
    }
}

You should also remove the Event.ADDED listener once the object has been added.

    private function beginClass(event:Event):void{
        //defining _root as the document root
        _root = MovieClip(root);
        this.removeEventListener(Event.ADDED, beginClass);
    }

OTHER TIPS

Could it be that the rest of the loop is trying to run still?

Try breaking out of the loop after the hit:

if (this.hitTestObject(_root.killGun))
{
    removeEventListener(Event.ENTER_FRAME, loop);
    _root.removeChild(this);
    return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top