In the previous article, we used cocos2d-x based MVC to make a simple game shelf, this shelf is still very simple, there are many things to be implemented.
Introduction Model
In the previous blog, we introduced view and controller. To implement the MVC mode, we also need to add a model class to maintain the game status. Our implementation should include the following classes:
1 gameboardview-that is, view,
2 gameboardcontroller-that is, controller.
3. gameboard-that is, model.
Model Implementation
Gameboard implementation
The requirements described in the first part are as follows:
... A game board is composed of N rows and n columns, which varies with the difficulty of the game.
Therefore, we implement the following encoding method:
Class GameBoard : public CCObject {public:int numberOfRows;int numberOfColums;void initGridSize(int aNumberOfRows, int aNumberOfColumns){this->numberOfColumns = aNumberOfColumns;this->numberOfRows = aNumberOfRows;}}
Please note that the model is inherited from ccbject-because the model only needs to focus on the status of the game board model (of course, there are also corresponding methods to update the status) --- we should not include other things. For example, it won't work if we inherit ccnode. We don't need ccnode things. Therefore, for the sake of purity, we inherit from game board here.
Implementation of gameboardview
Now we need to modify the view and contain a reference to the model. We can use the initwithgameboard method to initialize this member variable:
class GameBoardView :: public CCLayer {public:GameBoard *gameBoard;void initWithGameBoardModel(GameBoard *aGameBoard);}
The specific implementation details of gameboardview are as follows: (for convenience of demonstration, we ignore the code for actually rendering every small square)
void GameBoardView::initWithGameBoard(GameBoard *aGameBoard){if(gameBoard != NULL){gameBoard->release();gameBoard = NULL;}this->gameBoard = aGameBoard;this->gameBoard->retain();// render gameBoard backgroundCCSprite *gameBoardSprite = CCSprite::spriteWithFile("gameBoard.png");gameBoardSprite->setAnchorPoint(ccp(0, 0));this->addChild(gameBoardSprite);for(int i=0; i<gameBoard->numberOfRows; i++)for (int j=0; j<gameBoard->numberOfColumns; j++){// position and render game board spacse}}
Gameboardcontroller
Finally, we need to update the init method of gameboardcontroller, because view needs to inject the gameboard object through the init method. Therefore, we should define the model object in the init method of controller, and then pass it to the view.
Virtual bool Init () {bool Bret = false; do {// call the init method cc_break_if (! Cclayer: Init (); gameboard * gameboard = new gameboard (); gameboard-> initgridsize (7, 9); view = gameboardview: node (); view-> initwithgameboard (gameboard); this-> addchild (view, 0); Bret = true;} while (0) beturn Bret ;}
Handling touch events
Gameboardview updates
Although our view inherits the cclayer, the view itself should not process user interactions (touch events). Therefore, we need to define a proxy (gameboardviewdelegate ). (TRANSLATOR: Why is proxy defined here? The so-called proxy is, of course, what you don't want to do. If you want someone else to do it, this is the proxy. Therefore, when you write code, you want to maintain the simplicity and reusability of the class, and you can give the event to other classes as much as possible to do your own thing. That is, SRP, a single responsibility principle. If a class has too many points of attention, there are too many things to do. You can do these tasks directly or call other objects. If you do this yourself, it will complicate the functions of the class and make maintenance inconvenient. Calling other objects too much to do something seems good on the surface, but it is actually over-coupling. The principle of writing classes should be high cohesion and low coupling. Maybe you would say that if you don't use a proxy, do you give it to someone else? That's right. However, the proxy is an interface, and we program the interface, so its reusability will be very good. So next time you want to write scalable and reusable code, think about proxy. C ++ and Java are implemented using interfaces, while the delegate in objc is implemented using protocol. You can compare the specific conversion methods .)
class GameBoardViewDelegate{public:virtual void dealWithTouchesBegan(GameBoard *gameBoard, int row, int column) = 0;};
We also need to modify the init method of gameboardview and send a delegate to process the touch event.
void initWithGameBoard(GameBoard *aGameBoard, GameBoardViewDelegate *aDelegate);
The following is the specific implementation of the touch event. Remember to first setistouchenable (true) during initialization ^ _ ^:
Void gameboardview: cctouchesbegan (ccset * ptouches, ccevent * pevent) {cctouch * Touch = (cctouch *) ptouches-> anyobject (); If (touch = NULL) return; // coordinate replacement, which is equivalent to the following: // ccpoint location = touch-> locationinview (touch-> View (); // location = ccdirector: shareddirector () -> converttogl (location); ccpoint point = This-> converttouchtonodespace (touch); // calculate row and column touched by the user and call a delegate methodint ROW = 0; int column = 0 ;//... this-> gameboardviewdelegate-> dealwithtouchesbegan (gameboard, row, column );}
Gameboardcontroller update
Gameboardcontroller will be responsible for handling user touch events. Therefore, we need to enable gameboardcontroller to implement the gameboardviewdelegate interface:
class GameBoardController :public CCLayer, public GameBoardViewDelegate{public://...virtual void dealWithTouchesBegan(GameBoard *gameBoard, int row, int column){// do the game logic here and update view accordingly}// ...};
The last step is to modify the init method of the view and pass the Controller in.
// initialize viewview->initWithGameBoard(gameBoard, this);
Summary
In this article, we have implemented the model and used some code to associate the view with the Controller. At the same time, we also established a connection between the view and the controller and the user touch event, so that the controller class can respond to the user input in the game. (TRANSLATOR: it is very important that you spend so much effort on separation of duties !). In the following article, we will discuss the following two questions:
· Update the model in the controller,
· Notify the view about model changes.
Reference Source: http://www.cnblogs.com/andyque/archive/2012/03/18/2390102.html