一个鼠标拖出一个圆形的简单demo

好久没上来过了·
感觉自己都不属于这个圈子·~~




这里讲一个鼠标画圆的例子
其中最主要的是圆半径的求法
小学数学勾股定理
转换为计算机语言就是
var radius:Number = Math.sqrt(Math.pow(thisWidth, 2) + Math.pow(thisHeight, 2));


package
{
 import flash.display.*;
 import flash.events.*;
 import flash.display.Sprite;
 import flash.events.*;
 public class CircleDemo extends Sprite
 {

  private var _sprite:Sprite;
  private var pos1:Object;
  private var pos2:Object;

  public function CircleDemo()
  {
   _sprite=new Sprite();
   this.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
   this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

   //drawMyCircle(100,100,50);
  }

  private function drawMyCircle(pointX:Number,pointY:Number,radius:Number):void
  {
   _sprite.graphics.beginFill(0x88FB00,0.4);
   _sprite.graphics.lineStyle(2,0xFFFF00);     
   _sprite.graphics.drawCircle(pointX,pointY,radius);
   _sprite.graphics.endFill();
   this.addChild(_sprite);
   _sprite.addEventListener(MouseEvent.CLICK, clickHandler);
  }

  private function mouseDownHandler(event:MouseEvent):void
  {
   pos1 = {x:event.stageX, y:event.stageY};
  }


  private function mouseUpHandler(event:MouseEvent):void
  {
   pos2 = {x:event.stageX, y:event.stageY};

   var thisWidth:Number = (pos2.x - pos1.x);
   var thisHeight:Number = (pos2.y - pos1.y);
   var radius:Number = Math.sqrt(Math.pow(thisWidth, 2) + Math.pow(thisHeight, 2));
   drawMyCircle(pos1.x,pos1.y,radius);
  }

  private function clickHandler(event:MouseEvent):void
  {
   var thisMC:MovieClip = event.currentTarget as MovieClip;
   thisMC.graphics.clear();
  }
 }
}


 

posted on 2009-09-22 10:30  噢耶游戏  阅读(463)  评论(0编辑  收藏  举报