展会信息港展会大全

Cocos2d-x 3.2 大富翁游戏项目开发-第十三部分 购买空地,cocos2d-x项目开发
来源:互联网   发布日期:2015-09-28 14:47:30   浏览:2661次  

导读: Cocos2d-x 3.2 大富翁游戏项目开发-第十三部分 购买空地,cocos2d-x项目开发 先看一下角色各自的土地图片 1、先在场景中创建一个对话框,这个对话框是用来显示购买空地的...

Cocos2d-x 3.2 大富翁游戏项目开发-第十三部分 购买空地,cocos2d-x项目开发

先看一下角色各自的土地图片

\

1、先在场景中创建一个对话框,这个对话框是用来显示购买空地的确认信息的。buyLandCallback回调方法,根据点击的按钮进行分别处理,

如果确认是买地,则修改空地图片,如果取消则返回场景继续其他角色行走。buyLandCallback代码稍后再看

void GameBaseScene::initPopDialog()

{

popDialog = PopupLayer::create(DIALOG_BG);

popDialog->setContentSize(CCSizeMake(Dialog_Size_Width, Dialog_Size_Height));

popDialog->setTitle(DIALOG_TITLE);

popDialog->setContentText("", 20, 60, 250);

popDialog->setCallbackFunc(this, callfuncN_selector(GameBaseScene::buyLandCallback));

popDialog->addButton(BUTTON_BG1, BUTTON_BG3, OK, Btn_OK_TAG);

popDialog->addButton(BUTTON_BG2, BUTTON_BG3, CANCEL, Btn_Cancel_TAG);

this->addChild(popDialog);

popDialog->setVisible(false);

}

2、 我们把消息注册接收重新整理一下,统一写到一个方法中,这样便于后期扩展处理

void GameBaseScene::receivedNotificationOMsg(Object* data)

{

……………………

switch(retMsgType)

{

case MSG_BUY_BLANK_TAG: //处理购买空地

{

………………………

}

case MSG_GO_SHOW_TAG://处理go按钮显示

{

…………………

}

case MSG_GO_HIDE_TAG://处理go按钮消失

{

……………………..

}

}

}

3、 编写工具类Util,主要包含坐标转换 、 字符串截取相关方法。

struct Util

{

//把layer层上的坐标转换成GL坐标

static Point map2GL(const Point& ptMap, TMXTiledMap* map)

{

Point ptUI;

ptUI.x = ptMap.x * map->getTileSize().width;

ptUI.y = (ptMap.y + 1)* map->getTileSize().height;

Point ptGL = ptUI;

ptGL.y = map->getContentSize().height - ptUI.y;

return ptGL;

}

//把GL坐标 转换成layer上的坐标,这样layer层根据坐标就可以判断当前位置的title ID 并进行相关操作

static Point GL2map(const Point& ptGL, TMXTiledMap* map)

{

Point ptUI = ptGL;

ptUI.y = map->getContentSize().height - ptGL.y;

int x = ptUI.x / map->getTileSize().width;

int y = ptUI.y / map->getTileSize().height;

return ccp(x, y);

}

static Vector<String*> splitString(const char* srcStr, const char* sSep)

{

………………….//截取字符串相关

}

};

4、角色轮流行走近一步完善:添加变量oneRoundDone,标示一个回合是否结束,false是没有结束

逻辑同前面文章介绍基本一样,只不过是新加了个变量oneRoundDone,进行我方角色对话框的特殊处理。

当我方角色走完要求的步数之后,需要判断停留所在地是否有空地可以购买,有空地就发送消息,弹出对话框提示购买,购买或取消购买完毕后,发送一个MSG_PICKONE_TOGO_TAG ,从角色容器中取出一个角色,进行行走,全部角色行走完毕后,设置oneRoundDone为true,表示一个回合结束,此时在发送消息,显示go按钮,。

开始实现我方角色购买地皮的功能

void RicherGameController::endGo()

{

GameBaseScene::pathMarkVector.at(stepHasGone)->setVisible(false);

stepHasGone++;

if(stepHasGone >= stepsCount)

{

//角色停留后,判断如果是我方角色的时候 ,就调用handlePropEvent方法,进行道具事项的处理

//这里的道具暂时是指空地

if(_richerPlayer->getTag() == PLAYER_1_TAG)

{

handlePropEvent();

return ;

}

// oneRoundDone变量是指:一个回合是否结束。False是没有结束

if(!oneRoundDone)

{

//如果一个回合没有结束,就调用pickOnePlayerToGo从角色列表中取出一个行走

pickOnePlayerToGo();

return;

}

return;

}

currentRow = nextRow;

currentCol = nextCol;

moveOneStep(_richerPlayer);

log("go end");

}

void RicherGameController::handlePropEvent()

{

oneRoundDone =false;//设置回合变量oneRoundDone

float playerEnd_X = _colVector[stepsCount]*32; //取得角色所在地点的x坐标

float playerEnd_Y = _rowVector[stepsCount]*32 + 32; //取得角色所在地点的y坐标

float** positionAroundEnd; //定义个二维数组,存放角色当前位置的上下左右四个方向上的坐标

positionAroundEnd = new float*[4];

for(int i=0;i<4;i++)

positionAroundEnd[i]=new float [2];

// up

positionAroundEnd[0][0] = playerEnd_X;

positionAroundEnd[0][1] = playerEnd_Y + 32;

// down

positionAroundEnd[1][0] = playerEnd_X;

positionAroundEnd[1][1] = playerEnd_Y - 32;

// left

positionAroundEnd[2][0] = playerEnd_X - 32;

positionAroundEnd[2][1] = playerEnd_Y;

// right

positionAroundEnd[3][0] = playerEnd_X + 32;

positionAroundEnd[3][1] = playerEnd_Y;

log("handlePropEvent() called");

//寻找四个相邻位置 是否存在空地,如果有则发送要求买地的消息MSG_BUY,该消息包含了该空地的坐标,这里主要用到了字符串的操作,

//以“-”做为字符串分隔符

for (int i = 0; i < 4; i++)

{

// GL2map是把当前的坐标转换成landLayer层对应的坐标。这部分实现在了Util里面,不再做解释了

Point ptMap = Util::GL2map(Vec2(positionAroundEnd[i][0],positionAroundEnd[i][1]), GameBaseScene::_map);

int titleId = GameBaseScene::landLayer->getTileGIDAt(ptMap);

if(titleId == GameBaseScene::blank_land_tiledID)

{

//send a message to show buy dialog

float x = ptMap.x;

float y = ptMap.y;

String * str = String::createWithFormat("%d-%f-%f",MSG_BUY_BLANK_TAG,x,y);

NotificationCenter::getInstance()->postNotification(MSG_BUY,str);

break;

}

}

}

在GameBaseScene中注册了MSG_BUY买地的消息观察者,当收到该消息后调用receivedNotificationOMsg方法

void GameBaseScene::registerNotificationObserver()

{

NotificationCenter::getInstance()->addObserver(

this,

callfuncO_selector(GameBaseScene::receivedNotificationOMsg),

MSG_GO,

NULL);

NotificationCenter::getInstance()->addObserver(

this,

callfuncO_selector(GameBaseScene::receivedNotificationOMsg),

MSG_BUY,

NULL);

}

receivedNotificationOMsg 方法,处理所有接收到的消息,然后分别处理

void GameBaseScene::receivedNotificationOMsg(Object* data)

{

//首先对取出消息分割符“-”前的消息类型,并把消息存入容器中messageVector,对于没有分隔符的则只存放消息类型,

//对买地消息,还存放了地块的坐标值

String * srcDate = (String*)data;

Vector<String*> messageVector = Util::splitString(srcDate->getCString(),"-");

int retMsgType = messageVector.at(0)->intValue();

Vector<Node*> vecMenuItem = getMenu()->getChildren();

log("received go message is: %d",retMsgType);

switch(retMsgType)

{

case MSG_BUY_BLANK_TAG:

{

buy_land_x = messageVector.at(1)->floatValue();

buy_land_y = messageVector.at(2)->floatValue();

//当收到买地信息后,调用showBuyLandDialog方法,显示对话框

showBuyLandDialog(MSG_BUY_BLANK_TAG);

break;

}

case MSG_GO_SHOW_TAG:

{

…………………

}

case MSG_GO_HIDE_TAG:

{

……………………..

}

}

}

void GameBaseScene::showBuyLandDialog(int landTag)

{

String showMessage = "";

switch(landTag)

{

case MSG_BUY_BLANK_TAG:

{

//买空地 显示的消息

showMessage = "Do you want to buy the land ? need $1000 ";

break;

}

case MSG_BUY_LAND_1_TAG:

{

//给空地升级,显示的消息

showMessage = "Do you want to buy the land ? need $2000 ";

break;

}

}

popDialog->setDataTag(landTag);//给对话框设置一个标示,代表消息类型

popDialog->getLabelContentText()->setString(showMessage.getCString());//修改对话框提示内容

popDialog->setVisible(true);//对话框显示出来

}

//对话框点击后进入该方法

void GameBaseScene::buyLandCallback(Node *pNode)

{

if(pNode->getTag() == Btn_OK_TAG)

{

switch(popDialog->getDataTag())

{

case MSG_BUY_BLANK_TAG:

{

//如果点击的是确认,修改空地图片为角色对应的土地标示

landLayer->setTileGID(player1_building_1_tiledID,ccp(buy_land_x,buy_land_y));

//landLayer->setTileGID();

log( "need $1000 ");

break;

}

case MSG_BUY_LAND_1_TAG:

{

break;

}

}

popDialog->setVisible(false);//购买完毕后,让对话框消失,并发送消息,让其他角色行走

NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG));

log("Btn_OK_TAG");

}else

{

//点击取消,则让对话框消失,发送消息,让其他角色行走

popDialog->setVisible(false);

NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG));

}

}

效果如下

\

点击下载代码

http://download.csdn.net/detail/lideguo1979/8312731

未完待续........................

http://www.bkjia.com/Androidjc/934658.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/934658.htmlTechArticleCocos2d-x 3.2 大富翁游戏项目开发-第十三部分 购买空地,cocos2d-x项目开发 先看一下角色各自的土地图片 1、先在场景中创建一个对话框,这个...

赞助本站

人工智能实验室

相关热词: android开发 应用开发

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港