Flex2 用鼠标拖动控件

<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  layout ="absolute"  width ="427"  height ="374"  backgroundGradientColors ="[#ffffff, #ffffff]" >
< mx:Script >
    
<![CDATA[
        import mx.events.MoveEvent;
        private var oldX,oldY:Number;
        private  function onMouseMove(event:MouseEvent):void{
            lbl.text = "Local (x,y):" + event.localX.toString() + ","+event.localX.toString();
            lbl2.text = "Stage (x,y):" + event.stageX.toString() + ","+event.stageY.toString();
            if(event.buttonDown){
                var x:Number = event.stageX - oldX;
                var y:Number = event.stageY - oldY;
                oldX = event.stageX;
                oldY = event.stageY;                
                img.move(img.x + x,img.y + y);        
            }
        }
        
        private  function onMouseDown(event:MouseEvent):void{
            oldX = event.stageX;
            oldY = event.stageY;
        }
    
]]>
</ mx:Script >
    
< mx:Label  x ="10"  y ="10"  text ="Label"  id ="lbl" />
    
< mx:Label  x ="10"  y ="27"  text ="Label"  id ="lbl2" />
    
< mx:Image  id ="img"  x ="96"  y ="117"  source ="http://www.cnblogs.com/Images/adminlogo.gif"  mouseMove ="onMouseMove(event)"  mouseDown ="onMouseDown(event)" />
    
</ mx:Application >







你可能感兴趣的:(Flex)