How to Implement MVC in the Cocos2d-x (5)

Source: Internet
Author: User

This article is based on the previous two articles. If you have not read them, read the following two articles:

· How to Implement MVC in the cocos2d-x (3)

· How to Implement MVC in the cocos2d-x (4)

Update Model

When you select a small tool from the toolbox and place it on the game board, we need to encode and respond to these events. In the previous article, we have implemented the touchedatrow method of gameboardviewdelegate. We also need to add another interface method for this Protocol. As follows:

class GameBoardViewDelegate{public:     virtual void touchAtGrid(GameBoard *gameBoard, int row, int column) = 0; virtual void touchWithToolBoxItemAtIndex(GameBoard *gameBoard, int index) = 0;};

We need to modify the touch event processor so that we can determine whether we have touched the toolbox or game board.

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); // The following describes how to divide the width of the game zone and tool zone by 8: 2. ccsize size = ccdirector: shareddire () -> getwinsize (); ccrect * gameboardrectangle = new ccrect (0, 0, size. width * 0.8, size. height); ccrect * toolboxrectangle = new ccrect (size. width * 0.8, 0, size. width * 0.2, size. height); If (ccrect: ccrectcontainspoint (* gameboardrectangle, point) {// calculate row and column touched by the user and call a delegate method int ROW = 0; int column = 0 ;//... this-> gameboardviewdelegate-> touchatgrid (gameboard, row, column);} else if (ccrect: ccrectcontainspoint (* toolboxrectangle, point) {int Index = 0; // calculate toolbox item index based on a touch coordinate this-> gameboardviewdelegate-> touchwithtoolboxitematindex (gameboard, index );}}

Processing touch events in the Controller class is very simple. We only need to hold a reference to the model, and then call the Model Method Based on the touch event. Our interface looks similar to the following, but some implementation details are omitted:

class GameBoard : public CCObject{public:     // ...     GamePiece* getGamePieceFromToolBoxItemAtIndex(int index);public:     // ...     int selectedToolBoxItemIndex;};

Then, we fully implement the two gameboardviewdelegate methods in the gameboardcontroller.

void GameBoardController::touchAtGrid(GameBoard *gameBoard, int row, int column){     // if the toolbox item is selected move item from toolbox to game board     if(gameBoard->selectedToolBoxItemIndex != -1){          GamePiece *gamePiece = gameBoard->getGamePieceFromToolBoxItemAtIndex(gameBoard->selectedToolBoxItemIndex);          gameBoard->putGamePiece(gamePiece, row, column);     }}void GameBoardController::touchWithToolBoxItemAtIndex(GameBoard *gameBoard, int index) {    // keep the toolbox selection state in the Model    gameBoard->selectedToolboxItemIndex = index;}

So far, we have implemented that users can click the gadgets in the toolbox and place them on a small square in the game board. At the same time, the model class serves as a bridge in the middle.

Notify view about model changes

To reflect the State Changes of the model in the view, we can send a notification message to the view when the model changes. Then, the view can make different responses based on different messages. Like implementing the view through the Controller, here we also define a gameboarddelegate to notify the view model changes.

class GameBoardDelegate{public:     virtual void didPutGamePiece(GamePiece *gamePiece, int row, int column) = 0;};class GameBoard : public CCObject{     // ...public:    void setGameBoardDelegate(GameBoardDelegate *aGameBoardDelegate);private:     GameBoardDelegate *gameBoardDelegate;};void GameBoard::putGamePiece(GamePiece *gamePiece, int row, int column){     // ...        // store game piece         // notify that the game piece was put on a gameboard     this->gameBoardDelegate->didPutGamePiece(gamePiece, row, column);}void GameBoard::setGameBoardDelegate(GameBoardDelegate *aGameBoardDelegate){     this->gameBoardDelegate = aGameBoardDelegate;}

When implementing gameboarddelegate in gameboardview, when we need to place a small tool on the game board, we define a ccsprite.

class GameBoardView :public CCLayer, public GameBoardDelegate{     // ...};void GameBoardView::initWithGameBoard(GameBoard *aGameBoard, GameBoardViewDelegate *aDelegate){     // ...     this->gameBoard = aGameBoard;     this->gameBoard->retain();     this->gameBoard->setGameBoardDelegate(this);     this->gameBoardViewDelegate = aDelegate;     // ...}void GameBoardView::didPutGamePiece(GamePiece *gamePiece, int row, int column){     // create CCSprite and put it on a game board at corresponding position        CCSprite *gamePieceSprite = CCSprite::spriteWithFile("CloseNormal.png");     // ...     this->addChild(gamePieceSprite, 1);}

Summary

Now all the parts of the framework are linked. Model, view, and controller constitute the famous MVC pattern.

· View receives touch events and passes the events to the Controller,

· The Controller responds to the user's touch event and then updates the model.

· Model updates its own status, processes the game logic, and then tells the view what it has changed.

· View updates its display based on the current state of the model.

Reference Source: http://www.cnblogs.com/andyque/archive/2012/03/18/2390106.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.