2011-05-21 50 views
2

我對as3遊戲開發有點新鮮,但我想爲Connect Four遊戲創建一個有點靈活的基礎。我希望能夠對遊戲板和遊戲作品進行剝皮。這是我到目前爲止的想法。如果有人有建議我會很感激:面向AS3遊戲的OOP練習/結構

GameController擴展EventDispatcher - 包含所有遊戲網格操縱方法。 - 包括2D陣列以保持遊戲幣位置 的軌道 - 驗證之後調度事件時的方法被調用

GameClass擴展Sprite: - 承裝板 的視覺元素 - 的MouseEvent監聽器附接至視覺元素,其調用的控制器方法 - (自定義)ControllerEvent監聽器來更新視覺效果時GameController調度

遊戲幣類擴展雪碧: - 保存一塊的行/列位置 - 保存currentPlayer指數 - 加載PNG URL皮膚

這是粗略的輪廓。任何紅旗或其他建議非常感謝。

回答

3

這聽起來像GridController將遭受混合責任;在MVC體系結構中,控制器的職責是將數據從模型來回拖動到視圖。我個人認爲有將舉行底層多維數組,它表示將片網格和方法,例如GridModel:

public class GridModel extends EventDispatcher { 
    private var _grid : Array; 

    public function GridModel(rows : uint, cols : uint) : void { 
     // Create the data structure which represents the Grid. 
     _grid = initialiseGrid(rows, cols); 
    } 

    public function addPiece(player : uint, col : uint) : void { 
     if (isValidMove(col)) { 
      // Update the datastructure, determine which row the piece ended 
      // up residing in. 
      const row : uint = // method omitted 

      // Notify the rest of the system that a move has been made. 
      dispatchEvent(new GridUpdateEvent(GridUpdateEvent.MOVE, player, col, row, _grid.concat()); 
     } 
     else { 
      // Illegal Move, datastructure stays the same, notify the rest 
      // of the system. 
      dispatchEvent(new IllegalMoveEvent(IllegalMoveEvent.COLUMN_FULL, player, col, _grid.concat())); 
     } 
    } 
} 

您的控制器的主要作用現在將聽取由調度的事件模型,然後相應地更新View(DisplayList)。就像明智的一樣,你的視圖應該根據用戶交互來派發事件(例如:玩家之一表示他們希望將一塊物品放入第二列);控制器可以調用模型上的相關方法。

下面的代碼段應該給你一些關於控制器的職責是什麼的指示;不要忘記,您可以(也應該!)通過使用多個模型,視圖和(如果需要的話)控制器來降低您的責任。

public class GameController { 
    private var _gridModel : GridModel; 
    private var _stateModel : GameStateModel; 
    private var _gridView : GridView; 

    public function GameController(gridModel : GridModel, gameStateModel : GameStateModel, gridView : GridView) { 
     _gridModel = gridModel; 
     _gameStateModel : gameStateModel; 
     _gridView = gridView; 

     addEventListeners(); 
    } 

    private function addEventListeners() : void { 
     _gridModel.addEventListener(GridUpdateEvent.MOVE, onGridUpdateMoveEvent); 
     _gridView.addEventListener(ColumnSelectionEvent.SELECTED, onColumnSelectedEvent); 
    } 

    private function onColumnSelectedEvent(event : ColumnSelectionEvent) : void { 
     // Query the GameStateModel to find out whos turn it currently is. 
     const activePlayer : uint = _gameStateModel.activePlayer; 

     // Ask the GridModel to update. 
     _gridModel.addPiece(activePlayer, event.column); 
    } 

    private function onGridUpdateMoveEvent(event : GridUpdateEvent) : void { 
     // Update the view. 
     _gridView.insertPiece(event.player, event.row, event.col); 

     // Update the GameState to indicate it's the next player turns. 
     _gameSate.completeTurn(); 
    } 
} 
+0

這太棒了,謝謝我完全按照邏輯。我想爲GridModel和GameState創建單獨的類,但是我陷入了懶惰的舊陷阱,並且問'它真的需要它自己的類嗎?'即使在這個層面上,我當然也看到了解除這種關係的力量。 – ChickensDontClap 2011-05-21 12:23:55

+0

將GameStateModel與GridModel分開的一個原因是要改變玩家的數量; GridModel並不關心有多少人在玩遊戲,這是GameStateModel的責任。通過使用多態性,您可以輕鬆地在TwoPlayerGameStateModel和ThreePlayerGameStateModel之間進行切換。 – JonnyReeves 2011-05-21 14:17:05