2011-12-27 25 views
0

我想創建一個可以拖動塊的塊遊戲。我正在尋找最友好的拖拽行爲。它只能移動水平或垂直,並且只能移動1個塊。它應該捕捉(緩解到期望的目標位置),因爲你不能在中間釋放它(在塊之間)。它應該有點像iPhone開關按鈕,但十字架。我試圖通過檢查鼠標的角度到塊的原始位置來檢查它應該移動的方向。我希望它總是能夠穿過中心(原始位置)。我的意思是;它不能從左邊的位置動畫到上面的位置,它應該先動畫到中心。遊戲的交叉拖動行爲

這些塊是左上對齊的,塊之間有間距。

這樣做的最好方法是什麼? Drag behavior game example

UPDATE:
這是我當前的代碼。它目前正朝着這個方向發展,但並沒有動搖這一點。

package nl.stroep.games.behaviors 
{ 
    import flash.display.DisplayObject; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.geom.Point; 
    import nl.stroep.games.boxgame.Setting; 
    /** 
    * Target dragging behavior 
    * @author Mark Knol 
    */ 
    public class CrossDragBehavior 
    { 
     private static const HALF_PI:Number = Math.PI/2; 
     private static const QUART_PI:Number = HALF_PI/2; 

     private var _target:DisplayObject; 
     private var _targetPosition:Point; 

     public function CrossDragBehavior(target:DisplayObject) 
     { 
      _target = target; 

      _target.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse); 
      _target.stage.addEventListener(MouseEvent.MOUSE_UP, handleMouse); 
     } 

     private function handleMouse(e:MouseEvent):void 
     { 
      switch(e.type) 
      { 
       case MouseEvent.MOUSE_DOWN: 
       { 
        _targetPosition = new Point(_target.x, _target.y); 
        _target.addEventListener(Event.ENTER_FRAME, update); 
        update(null); 
        break; 
       } 
       case MouseEvent.MOUSE_UP: 
       { 
        _target.removeEventListener(Event.ENTER_FRAME, update); 
        break; 
       } 
      } 
     } 

     private function update(e:Event):void 
     { 
      var mouseX:Number = _target.parent.mouseX; 
      var mouseY:Number = _target.parent.mouseY; 

      var dx:Number = mouseX - _targetPosition.x; 
      var dy:Number = mouseY - _targetPosition.y; 
      var angle:Number = Math.atan2(dy, dx); 

      //trace(angle); 

      var directionX:int = 0; 
      var directionY:int = 0; 

      if (dx * dx + dy * dy > Setting.BLOCK_WIDTH * Setting.BLOCK_WIDTH) // minimum distance before snap 
      { 
       if (angle < 0 * HALF_PI - QUART_PI) { directionX = 0; directionY = -1; } 
       else if (angle < 1 * HALF_PI - QUART_PI) { directionX = 1; directionY = 0; } 
       else if (angle < 2 * HALF_PI - QUART_PI) { directionX = 0; directionY = 1; } 
       else if (angle < 3 * HALF_PI - QUART_PI) { directionX = -1; directionY = 0; } 
       else { directionX = 1; directionY = 0; } 
      } 

      _target.x = _targetPosition.x + (directionX * (Setting.BLOCK_WIDTH + Setting.BLOCK_DISTANCE)) 
      _target.y = _targetPosition.y + (directionY * (Setting.BLOCK_HEIGHT + Setting.BLOCK_DISTANCE)) 
     } 

     public function set target(value:DisplayObject):void 
     { 
      _target = value; 
     } 

    } 

} 

回答

1

要做到這一點,最好的方法可能是你自己可能必須找到的東西。

就像在任何應用程序中一樣,簡單地開始,將問題分解爲塊,嘗試識別問題,然後實施第一個結構,足夠靈活以便進行改進。

我不明白你的遊戲的每一個方面...

"cause you cannot release it in the middle" //middle of what, of the cross? 
    "I want it always to go move to the center." // how's that possible? 
    "It should only move horizontal or vertical, and 1 block position only" 
    "It should kinda behave like an iPhone switch button, but in a cross" 

就個人而言,我發現所有這些線路混亂。如果它只移動一個街區,它怎麼可能在十字架上?

拋開這些問題,您可以先創建一個Block類。

它需要做什麼?

- Move Up 
- Move Down 
- Move Left 
- Move Right 

這裏您可以選擇實現單個方法或每個運動的方法。你的選擇:)

如果你知道塊去哪裏,那麼很容易讓它抓住。使用補間的方法,您只需要一個目標點,一個延遲和一個緩動功能以使其平穩移動。

方向也不是太困難,在鼠標移動中,您可以輕鬆計算方向,當您有方向時,您就有目的地。如果你有目的地,你有方法。

所以總結起來,一些屬性&方法,你的格擋類可能有...

private var left:int = 0; 
    private var right:int = 1; 
    private var top:int = 2; 
    private var down:int = 3; 

    private var tweenDelay:Number = .5; 

    // event listeners 
    private function mouseDownListener(event:MouseEvent):void{}; 
    private function mouseUpListener(event:MouseEvent):void{}; 
    private function mouseMoveListener(event:MouseEvent):void{}; 

    //get a couple of coordinates, feed them to the method & 
    //return an int (or a String) 
    private function getDirection(params:Object):int; 

    private function tweenBlock(direction:String):void{} 

丟幾個布爾在限制你的格擋的動作,或使用矩形不要去越界。瞧! :)

我可能忘記或忽略了一些東西,但這應該讓你開始。另一方面,如果您已經知道上述所有內容並且卡住了,請告訴我們確切的問題是什麼,並且可能會更有幫助。

+0

對不起,這是因爲我的英語。這兩個答案很重要: 「因爲你不能在中間釋放它」 你不應該能夠在兩個塊之間釋放它,它應該捕捉到其中一個塊。 「 ' – 2011-12-27 20:37:02

+0

」我希望它總是移動到中心。「 它應該始終穿過原始位置。這是實際的問題。如果我將塊拖到左側(例如),那麼我的目標位置是左側塊。它應該移動到左邊。但是,當我沒有釋放鼠標並且像塊一樣向上移動/拖動塊時,塊應該不會向上移動,因爲您應該將鼠標移到右側(回到中間位置),而不是拖動塊它起來。 – 2011-12-27 20:37:08

+0

順便說一句Ive用代碼更新了這個問題。 – 2011-12-27 20:51:19