cocos2dx教程

本文基于前面两篇文章,如果您还没有看过,建议先阅读下面两篇文章:

· cocos2d-x里面如何实现mvc(三)

· cocos2d-x里面如何实现mvc(四)

更新Model

当用户从工具箱中选一个小工具,然后把它放置到game board上面去时,我们需要编码响应这些事件。在上一篇文章中,我们已经实现了GameBoardViewDelegate的touchedAtRow方法。我们还需要给这个协议再添加一个接口方法。如下所示:

class GameBoardViewDelegate

{

public:

virtual void touchAtGrid(GameBoard *gameBoard, int row, int column) = 0;

virtual void touchWithToolBoxItemAtIndex(GameBoard *gameBoard, int index) = 0;

};

我们需要修改touch事件处理器,这样就可以判断我们到底是触摸了工具箱还是game board。

void GameBoardView::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)

{

CCTouch *touch = (CCTouch*) pTouches->anyObject();

if(touch == NULL)

return;

// 置换坐标,等效如下

//CCPoint location = touch->locationInView(touch->view());

//location = CCDirector::sharedDirector()->convertToGL(location);

CCPoint point = this->convertTouchToNodeSpace(touch);

// 下面假设游戏区与工具区8:2来划分宽度

CCSize size = CCDirector::sharedDirector()->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);

}

}

在controller类里面处理touch事件是非常简单的,我们只需要持有一个model的引用,然后基于touch事件来调用model的方法就行了。我们的接口看起来和下面差不多,只是省略掉了一些实现细节:

class GameBoard : public CCObject

{

public:

// …

GamePiece* getGamePieceFromToolBoxItemAtIndex(int index);

public:

// …

int selectedToolBoxItemIndex;

};

然后,我们在GameBoardController里面完全实现GameBoardViewDelegate的两个方法。

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;

}

到目前为止,我们实现了,用户可以点击工具箱中的小工具,然后把它们放置到game board中的一个小方块上面,同时model类在中间起了桥梁作用。

通知view关于model的改变

为了在view里面反映出model的状态更改,我们可以在model有变化的时候给view发送通知消息,然后view就可以根据不同的消息来作出不同的响应了。和我们在实现view通过controller一样,这里我们也定义了一个GameBoardDelegate,用来通知view model的变化。

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;

}

在GameBoardView里面实现GameBoardDelegate的时候,当我们需要在game board上面放置一个小工具的时候,我们定义了一个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);

}

总结

现在框架中所有的部分都联系起来了,model、view和controller三者组成了著名的MVC模式

· View接收touch事件,然后把事件传递给controller,

· Controller 响应用户的touch事件,然后更新model

· model 更新它自身的状态, 处理游戏逻辑,然后告诉view它改变了哪些东西。

· View则基于Model当前的状态来更新自己的显示

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:dandanxi6@qq.com

(0)
上一篇 2023年 8月 25日 下午2:10
下一篇 2023年 8月 25日 下午2:22

相关推荐

  • 小米手机10s原件扫描电子版怎么弄

    大家知道吗?小米手机其实拥有很多好用的功能,比如其中的扫描功能,非常好用。那么今天我们一起来了解一下小米手机的扫描功能吧,下面就一起来详细看看吧! 一、小米【扫一扫】 1.扫文档 …

    2023年 2月 21日
  • 怎么备份,导出和恢复微信聊天记录

    大家好,打工人陈蛋蛋又来营业了,这是今天微信专题的第二篇文章,今天讲讲微信聊天记录。 其实写这篇文章我也考虑了很久,因为经常看到有人在群里问关于怎么备份微信聊天记录啊,被删除的聊天…

    2023年 2月 24日
  • wps页眉横线怎么去掉

     在使用wps编辑文档的时候,在给文档进行页眉页脚的设置时,页眉部分会自动生成横线,如果我们觉得不太好看,想要将其删除的话,要怎么做才行呢?wps页眉横线怎么去掉?下面小编就为大家…

    2023年 7月 28日
  • 警方提醒开学季这些套路

    临近开学季 同学们又将陆续回到校园 此时,大家的警惕心 可千万不能松懈 真实案例 近日,身为班主任的李老师正在为新学期做准备工作时,突然接到一位家长的电话,对方在电话里非常着急说:…

    2023年 9月 8日
  • 5G和4G的区别在哪里

    4G(LTE)和5G(NR)都是移动无线接入技术,可以为移动设备提供以太网的速度,体验3D画质的视频播放服务。其中:LTE和WiMAX是实现4G定义的两种不同技术;而5G(NR)是…

    2023年 5月 24日
  • 微信亲属卡你用过没,微信亲属卡该怎么用

    微信“亲属卡”功能你用过吗? 注意!!! 诈骗分子盯上了这个功能 已经有不少人中招 什么是微信“亲属卡”? 顾名思义 微信用户 可以向指定亲属好友 开通“亲属卡”服务 让对方获得使…

    2023年 1月 19日
  • 腾讯广告怎么开户,腾讯广告开户费用多少

    开户基本流程 需注意: 1.已经关联的QQ帐号不可重复关联。 2.在选择“企业”或是“个人”后,账户属性不可再次修改。 特殊流程指引 h5应用场景 特殊流程指引是指部分特殊行业、产…

    2023年 7月 16日
  • 怎样用ps把图片变成圆形(图片变圆形方法ps)

      从功能上看,PS可分为图像编辑、图像合成、校色调色及功能色效制作部分等。 图像编辑是图像处理的基础,可以对图像做各种变换如放大、缩小、旋转、倾斜、镜像、透视等;也可进行复制、去…

    2023年 4月 2日
  • 蓝底的照片怎么变成白底(怎么样将白底照片变成蓝底)

    我们平时经常会用到证件照,但是每一次的拍摄基本都只能选择一种底色,如果需要其他底色的照片要怎么办呢?方法很简单,我们只要下载几款软件就能够快速解决啦,下面小编就来和大家举一个例子,…

    2023年 1月 9日
  • 专利无效宣告的法律后果

    北京超成律师事务所(以下简称“超成律所”)精选成功代理案例发布, 本期发布生活用品行业专利无效宣告答辩案例集锦(第一期),其他行业案例集锦将陆续推出,望持续关注。 本期案例速递 案…

    2023年 6月 12日