lundi 4 mai 2015

How to remove a child from a random array?

I have an array that randomly creates 10 dots. However there's a certain area where I do not want them to be created. How can I achieve this? My code gives me error 2025.

"The supplied DisplayObject must be a child of the caller."

It will occasionally output the totalDots as instructed, (trace""+totalDots), but 90% of the time it will give me the error.

        public var numDots:Array = [];
        public var totalDots:int = numDots.length;
        public var box:Box = new Box();

        public function addBox():void
        {
            box.x = stageWidth/2;
            box.y = stageHeight/2;
            addChild(box);
        }
        private function addDot():void
        {

            for(var i:int = 0; i < 10; i++)
            {
                var dot:Dot = new Dot();
                dot.x = Math.floor(Math.random() * stageWidth);
                dot.y = Math.floor(Math.random() * stageHeight);
                this.addChild(dot);
                totalDots++;
                trace(""+totalDots);

                for(var j:int = 0; j < totalDots; j++)
                {
                    if(numDots[j].hitTestObject(box))
                    {
                        stage.removeChild(numDots[j]);
                        numDots.splice(j, 1);
                        totalDots--;
                    }
                }
            }

        }




Aucun commentaire:

Enregistrer un commentaire