2012-02-21 70 views
1

我在嘗試製作一個可拖動的按鈕,並將它放到屏幕上。我知道我必須爲按鈕使用mouseDown和mouseUp選項,但是有人可以解釋一種允許我拖動它然後扔掉它的方法嗎?換句話說,它應該在mouseUp之後繼續前進。在Flash/AS3中投擲一個按鈕

預先感謝您能提供任何意見 - 克里斯

回答

1

有趣的是,我已經躺在附近的老班這一點。只需使用下面的任何對象的基類,你希望能夠到處亂扔:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    /** 
    * Use as a base class for objects to be grabbed and thrown with the mouse. 
    * @author Marty Wallace. 
    * @version 1.01. 
    */ 
    public class BouncingObject extends Sprite 
    { 

     // properties 
     public var yv:Number = 0; 
     public var xv:Number = 0; 

     private var _grabbed:Boolean = false; 
     private var _gy:int = 0; 
     private var _gx:int = 0; 
     private var _ox:int = 0; 
     private var _oy:int = 0; 

     protected var bounce:Number = 0.3; 
     protected var weight:Number = 3; 
     protected var friction:Number = 2; 


     /** 
     * Constructor 
     */ 
     public function BouncingObject() 
     { 
      addEventListener(Event.ADDED_TO_STAGE, _init); 
     } 


     /** 
     * Called on dispatch of Event.ADDED_TO_STAGE 
     * @param e Event.ADDED_TO_STAGE 
     */ 
     private function _init(e:Event):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, _init); 

      addEventListener(Event.ENTER_FRAME, _handle); 
      addEventListener(MouseEvent.MOUSE_DOWN, _handleClick); 
      addEventListener(MouseEvent.MOUSE_UP, _handleRelease); 
      addEventListener(MouseEvent.MOUSE_OUT, _handleRelease); 
     } 


     /** 
     * Called on dispatch of Event.ENTER_FRAME 
     * @param e Event.ENTER_FRAME 
     */ 
     private function _handle(e:Event):void 
     { 
      _ox = x; 
      _oy = y; 

      yv += weight; 

      if(_grabbed) 
      { 
       x = parent.mouseX - _gx; 
       y = parent.mouseY - _gy; 
      } 
      else 
      { 
       x += xv; 
       y += yv; 
      } 

      if(x < 0) 
      { 
       x = 0; 
       xv = -xv*bounce; 
       yv -= yv/friction; 
      } 
      if(x + width > stage.stageWidth) 
      { 
       x = stage.stageWidth - width; 
       xv = -xv*bounce; 
       yv -= yv/friction; 
      } 
      if(y < 0) 
      { 
       y = 0; 
       yv = -yv*bounce; 
       xv -= xv/friction; 
      } 
      if(y + height > stage.stageHeight) 
      { 
       y = stage.stageHeight - height; 
       yv = -yv*bounce; 
       xv -= xv/friction; 
      } 
     } 


     /** 
     * Called on dispatch of MouseEvent.MOUSE_DOWN 
     * @param e MouseEvent.MOUSE_DOWN 
     */ 
     private function _handleClick(e:MouseEvent):void 
     { 
      grabbed = true; 
      parent.addChild(this); 
     } 


     /** 
     * Called on dispatch of MouseEvent.MOUSE_UP 
     * @param e MouseEvent.MOUSE_UP 
     */ 
     private function _handleRelease(e:MouseEvent):void 
     { 
      grabbed = false; 
     } 


     /** 
     * Sets grabbed 
     * @param val Boolean representing value to set grabbed as 
     */ 
     protected function set grabbed(bool:Boolean):void 
     { 
      _grabbed = bool; 

      if(_grabbed) 
      { 
       _gx = mouseX; 
       _gy = mouseY; 
      } 
      else 
      { 
       xv = x - _ox; 
       yv = y - _oy; 
      } 
     } 


     /** 
     * Deconstructor - remove event listeners. 
     */ 
     public function deconstruct():void 
     { 
      removeEventListener(Event.ENTER_FRAME, _handle); 
      removeEventListener(MouseEvent.MOUSE_DOWN, _handleClick); 
      removeEventListener(MouseEvent.MOUSE_UP, _handleRelease); 
      removeEventListener(MouseEvent.MOUSE_OUT, _handleRelease); 
     } 

    } 
} 

隨意改變任何你需要的,例如將weight設置爲0將防止物體掉落。