mercredi 23 novembre 2016

ArgumentError: Removing Child from Stage

I am trying to develop a basic Snake game that involves some inverse kinematics and state machines. I'm trying to get it so when the first segment of the snake interacts with a certain "mouse", it disappears. However, when I do, it doesn't work and I end up getting ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

Here's my code:

package 
{
        import agent.Agent;
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.events.KeyboardEvent;
        import flash.geom.Point;
        public class Main extends Sprite 
        {
                private var Agents:Vector.<Agent>;
                                private var segments:Array;
                private var numSegments:uint = 150;
                private var player:Point = new Point (15, 15)
                public function Main():void 
                {
                        if (stage) init();
                        else addEventListener(Event.ADDED_TO_STAGE, init);
                }
                
                private function init(e:Event = null):void 
                {
                        stage.align = StageAlign.TOP_LEFT;
                        stage.scaleMode = StageScaleMode.NO_SCALE;
                        segments = new Array();
                        for(var i:uint = 0; i < numSegments; i++)
                        {
                                var segment:Segment = new Segment (5, 10);
                                addChild(segment);
                                segments.push(segment);
                        }
                        //updatePoint();
                        addEventListener(Event.ENTER_FRAME, onEnterFrame);

                        removeEventListener(Event.ADDED_TO_STAGE, init);
                        stage.scaleMode = StageScaleMode.NO_SCALE;
                        stage.align = StageAlign.TOP_LEFT;
                        // entry point
                        graphics.beginFill(0xeeeeee);
                        graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
                        
                        Agents = new Vector.<Agent>();
                        addEventListener(Event.ENTER_FRAME, gameloop);
                        for (var x:int = 0; x < 10; x++) 
                        {
                                var a:Agent = new Agent();
                                addChild(a);
                                Agents.push(a);
                                a.x = Math.random() * 10;
                                a.y = Math.random() * 10;
                        }
                        stage.addEventListener(MouseEvent.CLICK, createAgent);
                }
                
                private function createAgent(e:MouseEvent):void 
                {
                        var a:Agent = new Agent();
                        stage.addChild(a);
                        Agents.push(a);
                        a.x = mouseX;
                        a.y = mouseY;
                }
                
                private function gameloop(e:Event):void 
                {
                        for each (var a: Agent in Agents) {
                                a.update();
                                trace ("Follow me on Twitter.");
                                for each(var target: Segment in segments)
                                {
                                        if (target.hitTestPtarget.x, a.y + target.y, true))
                                        {
                                                stage.removeChild(a);
                                        }
                                }
                                
                        }
                }
                private function onEnterFrame(event:Event):void
                {
                        drag(segments[0], player.x, player.y);
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

                        for(var i:uint = 1; i < numSegments; i++)
                        {
                                var segmentA:Segment = segments[i];
                                var segmentB:Segment = segments[i - 1];
                                drag(segmentA, segmentB.x, segmentB.y);
                        }
                }
                private function keyDown (evt: KeyboardEvent): void {
                        //87=w 68=d 83=s 65=a
                        if (evt.keyCode == 87) 
                        {
                                player.y-=50;
                        } 
                        else if (evt.keyCode == 83) 
                        {
                                player.y+=50;
                        } 
                        else if (evt.keyCode == 68) 
                        {
                                player.x+=50;
                        } 
                        else if (evt.keyCode == 65) 
                        {
                                player.x-=50;
                        }
                        trace (player.x + " " + player.y);
                }
                private function drag(segment:Segment, xpos:Number, ypos:Number):void
                {
                        
                        var dx:Number = xpos - segment.x;
                        var dy:Number = ypos - segment.y;
                        var angle:Number = Math.atan2(dy, dx);
                        segment.rotation = angle * 180 / Math.PI;
                        
                        var w:Number = segment.getPin().x - segment.x;
                        var h:Number = segment.getPin().y - segment.y;
                        segment.x = xpos - w;
                        segment.y = ypos - h;
                }

                
        }
}

I have tried looking at other posts with the same topic, but I just don't understand what I'm doing wrong. Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire