绘制刚体(二)

与第一个绘制刚体例子一样的,在舞台上点击鼠标拖动,绘制刚体。舞台上有两个RadioButton,用来选择是画矩形的刚体还是圆形的刚体。

下面是mouseUp事件监听处理函数:

    private function onUp(e:MouseEvent):void 
		{
			_bodyContainer.graphics.clear();
			_isDown = false;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			
			var endx:int = stage.mouseX;
			var endy:int = stage.mouseY;
			
			var width:int;
			var height:int;
			
			var random:int = 1 + Math.floor(Math.random()*10);
			if (_isDrawRect)
			{
				if (endx<_initx)
				{
					var tempx:int = _initx;
					_initx = endx;
					endx = tempx;
				}
				if (endy<_inity)
				{
					var tempy:int = _inity;
					_inity = endy;
					endy = tempy;
				}
				width = Math.abs(endx - _initx);
				height = Math.abs(endy - _inity);
				if (width == 0 || height == 0) return;
				var myRect:Rect = new Rect(width, height, new Point(_initx + width / 2, _inity + height / 2), new Point(), new MyRect(), _bodyContainer);
				(myRect.displayObject as MovieClip).gotoAndStop(random);
				_actors.push(myRect);
			}
			else
			{
				width = Math.abs(endx - _initx);
				height = Math.abs(endy - _inity);
				if (width == 0 || height == 0) return;
				var dist:Number = Math.sqrt(width * width + height * height);
				trace(dist, width, height);
				var myCircle:Ball = new Ball(dist, new Point(_initx, _inity), new Point(), new MyCircle(), _bodyContainer);
				(myCircle.displayObject as MovieClip).gotoAndStop(random);
				_actors.push(myCircle);
			}
		}

posted @ 2010-04-17 00:00  ywxgod  阅读(298)  评论(0编辑  收藏  举报