2012-06-18 156 views

回答

1

在這裏,讓你開始...可拖動紅色圓圈

enter image description here

myApp.mxml在:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:comps="*"> 
    <comps:MyCircle width="100%" height="100%"/> 
</s:Application> 

MyCircle.mxml:

<?xml version="1.0" encoding="utf-8"?> 
<mx:UIComponent 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 

    <fx:Script> 
     <![CDATA[ 
      import flash.filters.*; 

      public static const SHADOW:Array = [ new DropShadowFilter(10, 80, 0x000000, 0.5, 32, 32, 1, 1, false, false, false) ]; 
      private var dX:Number, dY:Number; 
      private var circle:Shape = new Shape(); 

      override protected function createChildren():void { 
       super.createChildren(); 
       circle.graphics.beginFill(0xFF0000); 
       circle.graphics.drawCircle(0, 0, 20); 
       addChild(circle); 
       addEventListener(MouseEvent.MOUSE_DOWN, handleDown); 
      } 

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 
       super.updateDisplayList(unscaledWidth, unscaledHeight); 
       circle.x = unscaledWidth/2; 
       circle.y = unscaledHeight/2; 
      } 

      private function handleDown(event:MouseEvent):void { 
       dX = circle.x - stage.mouseX; 
       dY = circle.y - stage.mouseY; 
       circle.scaleX = circle.scaleY = 1.5; 
       circle.filters = SHADOW; 
       stage.addEventListener(MouseEvent.MOUSE_MOVE, handleDrag); 
       stage.addEventListener(MouseEvent.MOUSE_UP, handleUp); 
      } 

      private function handleDrag(event:MouseEvent):void { 
       circle.x = stage.mouseX + dX; 
       circle.y = stage.mouseY + dY; 
       event.updateAfterEvent(); 
      } 

      private function handleUp(event:MouseEvent):void { 
       circle.filters = null; 
       circle.scaleX = circle.scaleY = 1; 
       stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleDrag); 
       stage.removeEventListener(MouseEvent.MOUSE_UP, handleUp); 
      } 
     ]]> 
    </fx:Script> 
</mx:UIComponent>