ArgumentError:从舞台中删除子项

时间:2016-11-22 21:18:23

标签: actionscript-3 actionscript inverse-kinematics kinematics

我正在尝试开发一种基本的Snake游戏,它涉及一些反向运动学和状态机。我试图这样做,当蛇的第一部分与某个"鼠标相互作用时,它就消失了。但是,当我这样做时,它不起作用,我最终得到ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子节点。

这是我的代码:



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

		
	}
}
&#13;
&#13;
&#13;

我曾尝试查看同一主题的其他帖子,但我只是不明白我做错了什么。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您正在本地添加代理和细分(在Main中),稍后您尝试将它们从舞台中移除(这是swf的顶层,是另一个显示对象)。

在init函数中执行stage.addChild(...)或者(这是一个更好的选项imo)用其他地方的removeChild(...)替换stage.removeChild(...) - 这将保持本地主空间中的对象(谁知道,也许你想要稍后在你的主要区域移动,让所有东西一下子看不见等)。