As3:在命中测试中删除以编程方式添加的子项会产生错误#1009

时间:2014-02-19 22:47:28

标签: actionscript-3 hittest removechild

我尝试在与某个对象发生碰撞时删除以编程方式添加的动画片段。当他们这样做时,他们就会消失。但是在它们消失之后,我得到一个#1009错误。

错误指向第95行,即

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

有时在59岁,这是

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

为什么它一直试图运行第95行(有时候)59?我删除了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

    }

}

2 个答案:

答案 0 :(得分:0)

可能是循环的其余部分仍在尝试运行吗?

尝试在点击后退出循环:

if (this.hitTestObject(_root.killGun))
{
    removeEventListener(Event.ENTER_FRAME, loop);
    _root.removeChild(this);
    return;
}

答案 1 :(得分:0)

错误正在发生,因为您正在移除eventListener函数中的eventListener,并且您从父级移除对象并仍然处理该对象不再包含或已被取消的数据。您应该添加boolean来判断hitTest是否已经发生,然后不再处理循环。例如:

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);
    }
}

添加对象后,您还应删除Event.ADDED侦听器。

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