2012-02-21 77 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将防止物体掉落。