2012-02-21 4 views
1

ボタンをドラッグ可能にして、画面全体にスローするようにしています。私は、ボタンのmouseDownとmouseUpオプションを使用しなければならないことを知っていますが、誰かがドラッグしてスローする方法を説明できますか?言い換えれば、それはmouseUpの後に続けるべきです。あなたが提供できるすべてのアドバイスを事前にFlash/AS3でボタンをスローする

おかげ - Funnily十分

答えて

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); 
     } 

    } 
} 

は、例えば、何が必要変える気軽にweight0に設定すると、オブジェクトが落ちるのを防ぎます。

関連する問題