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

相关推荐

  • mp4太大怎么压缩不影响画质

    日常生活中视频文件发送已经十分常见,但可能会遇到文件太大无法发送或者超过网站上传限制大小的情况,这个时候就需要将原视频文件进行压缩,让视频文件变小从而顺利发送。 那mp4视频太大了…

    2023年 7月 31日
  • windows 10激活密钥(windows10激活密钥免费2020)

    Windows 10 系列 MAK激活密钥: Windows 10 Enterprise MAK激活密钥: [Key]:GQ837-N283G-P8XD4-7HGYJ-K2FCF …

    互联网 2023年 1月 21日
  • 除了起点中文网,还有什么发布原创小说的平台吗

    一:起点中文网 创立于2001年11月,起点作为国内最大文学阅读与写作平台之一,已经成为目前国内领先的原创文学门户网站,并创立了以“起点中文”为代表的原创文学领导品牌,建立了完善的…

    2023年 3月 18日
  • 让没有入网许可的山寨机顺利入网

    “消失”数年的山寨机通过直播间重回公众视野。近日,不少网友反映,在知名主播“驴嫂平荣”直播间购买的手机出现了冒用正规产品入网证号等问题,也就是说这些在直播间出售的手机并没有取得入网…

    2023年 2月 26日
  • 支付宝11年后的变化

    今天是2019年12月8日,也是支付宝15岁生日,15年前的今天,支付宝正式诞生,深刻地改变了国人生活的方方面面。 近日,有网友晒出了一组10后看不懂的影视片段,唤醒了80、90后…

    互联网 2023年 4月 12日
  • 翻译图片识别在线,如何使用翻译器扫描图片识别翻译

    翻译图片识别在线的方法是什么?如今,随着互联网技术和人工智能的发展,翻译图片识别已经成为一种便捷的在线翻译方式。翻译图片识别已经成为一种非常方便的在线翻译方式。在选择翻译图片识别的…

    2023年 5月 25日
  • 张杰和杨颖谁人气最高,张杰杨颖互动

    普通人或是明星,都避免不了取外号的经历,有的外号可爱讨喜,有的外号就有些“直白”,下面,小编带大家一起讨论一下娱乐圈的八大明星的外号吧! 张杰因为土土的穿着风格,被网友戏称为“闰土…

    2023年 7月 26日
  • 贴吧发帖引流最快的方法

    贴吧引流其实并不难,总结出来就那么几招,你掌握了它的一套操作流程,后期的运营只需维护好就没啥子问题。当然,在我们操作之前,你必须熟悉你关注的每个贴吧的规则,以免后期发帖出现各种麻烦…

    2023年 3月 1日
  • 家教注册什么公司(怎么注册家教网站)

    当我们上网多了,很多网站需要你注册,注册的时候就可能要你填写你的联系方式,比如手机号码,有的还要求提供邮箱地址;提供手机号码一般是跟实名认证有关系,也是当你的账号出现问题的时候,可…

    互联网 2023年 4月 24日
  • 今日头条今年有生肖集卡活动吗

    2月8日凌晨,今日头条推出的春节瓜分红包活动之一的“集生肖分2亿”活动正式开启,记住:集齐“12生肖+发财”一共14张卡片后,即可在15日晚瓜分2亿现金哦,不过小编的想法和大家都一…

    2023年 3月 17日