Cocos2d-x 3.2 大富翁游戲項目開發-第二十三部分 購買彩票
來源:程序員人生 發布時間:2015-02-02 08:19:34 閱讀次數:2526次
當角色途經彩票的標志或停留位置有彩票標志時,彈出購買彩票的對話框,提示購買彩票,已買過的號碼,不顯示。當機器對手途經時則直接購買彩票。

1、
在RicherPlayer.h中增加std::vector<int> lottery_vector;用來存儲購買的彩票號碼
2、
RicherGameController 修改endGo方法,每走完1步就會進入該方法,判斷是不是有彩票標示圖標,有的話發送MSG_LOTTERY彩票消息,MOVEPASS標示走完1步的標志
void RicherGameController::endGo()
{
...................
Size titleSize = GameBaseScene::wayLayer->getLayerSize();
int passId = GameBaseScene::wayLayer->getTileGIDAt(Point(currentCol,titleSize.height-currentRow⑴));
if(passId == GameBaseScene::lottery_tiledID)
{
String * str = String::createWithFormat("%d-%f-%f-%d-%d",MSG_LOTTERY_TAG,1.0f,1.0f,_richerPlayer->getTag(),MOVEPASS);
NotificationCenter::getInstance()->postNotification(MSG_LOTTERY,str);
return;
}
..............
}
3、
角色行走終了后,判斷停留地點是不是有彩票標志,有的話發送MSG_LOTTERY彩票消息,GOEND標示行走終了的標志
void RicherGameController::handlePropEvent()
{
...............
if(endId == GameBaseScene::lottery_tiledID)
{
String * str = String::createWithFormat("%d-%f-%f-%d-%d",MSG_LOTTERY_TAG,pointInMap.x,pointInMap.y,_richerPlayer->getTag(),GOEND);
NotificationCenter::getInstance()->postNotification(MSG_LOTTERY,str);
return;
}
...............
}
4、新建彩票號碼類LotteryCard ,該類里面的按鍵是菜單按鈕
void LotteryCard::cardInit(int numbers,int width,int height,float CardSpriteX,float CardSpriteY)
{
//設置初始化值
lotteryNumber = numbers;
//背景色彩
layerColorBG = LayerColor::create(Color4B(100,100,100,255),width⑸,height⑸);
layerColorBG->setPosition(Point(CardSpriteX,CardSpriteY));
if(lotteryNumber > 0)
{
//創建menuitem,設置其tag為彩票的號碼
ballMenuImage = MenuItemImage::create("images/lt_defalut_ball.png", "images/lt_blueball.png", this, menu_selector(LotteryCard::ballButtonCallback));
ballMenuImage->setTag(lotteryNumber);
ballMenuImage->setPosition(Point(layerColorBG->getContentSize().width/2,layerColorBG->getContentSize().height/2));
// 添加文字說明并設置位置
labelLotteryNumber = LabelTTF::create(String::createWithFormat("%i",lotteryNumber)->getCString(),"HiraKakuProN-W6",25);
labelLotteryNumber->setColor(Color3B(200,200,200));
labelLotteryNumber->setPosition(Point(layerColorBG->getContentSize().width/2,layerColorBG->getContentSize().height/2));
ballMenuImage->addChild(labelLotteryNumber);
//創建menu,添加menuitem
Menu* menu = Menu::create();
menu->setPosition(CCPointZero);
layerColorBG->addChild(menu);
menu->addChild(ballMenuImage);
//該彩票sprite的tag也是彩票號碼
this->setTag(lotteryNumber);
}
this->addChild(layerColorBG);
}
//調用彩票菜單按鍵的回調函數
void LotteryCard::ballButtonCallback(CCObject* pSender)
{
Node* node = dynamic_cast<Node*>(pSender);
ballMenuImage->selected();
if (m_callback && m_callbackListener)
{
(m_callbackListener->*m_callback)(node);
}
}
//設置菜單為不選中狀態
void LotteryCard::setUnSelected()
{
ballMenuImage->unselected();
}
//設置彩票菜單按鍵的回調函數
void LotteryCard::setBallCallbackFunc(cocos2d::Object *target, SEL_CallFuncN callfun)
{
m_callbackListener = target;
m_callback = callfun;
}
//返回彩票號碼
int LotteryCard::getLotteryNumber()
{
return lotteryNumber;
}
//設置彩票號碼
void LotteryCard::setLotteryNumber(int num)
{
lotteryNumber = num;
if(lotteryNumber > 0)
{
labelLotteryNumber->setString(String::createWithFormat("%i",lotteryNumber)->getCString());
}
}
5、修改PopupLayer類,添加彩票布局
在PopupLayer.h頭文件中添加對話框類型枚舉
enum POP_TYPE
{
NORMAL,//普通對話框
LOTTERY,//彩票對話框
STOCK, //留作后面的股票對話框
};
void setLotteryContext(Size size); //設置彩票號碼布局內容
POP_TYPE pop_type; //當前對話框類型
void setPopType(POP_TYPE type); //設置當前對話框類型
Vector<LotteryCard*> lotteryVector; //所有彩票號碼容器
void refreshBallBackGround(Node *pNode); //點擊1個彩票號碼后,更新其他彩票背景為不選中
int lottery_selected; //選中的彩票號碼
std::vector<int> selected_number_vector; //已選擇的彩票號碼容器
void setHasSelectedLotteryNumber(std::vector<int> _vector); //設置已選擇的彩票號碼
看具體的實現
bool PopupLayer::init()
{
.........
lottery_selected = 0;//初始化時,沒有選中的號碼
..........
}
void PopupLayer::onEnter()
{
............
switch(pop_type)
{
case LOTTERY:
{
//如果對話框是彩票類型,則添加彩票布局
setLotteryContext(contentSize);
break;
}
case STOCK:
{
break;
}
default:
{
// 此處表示普通對話框,則只顯示文本內容
if (getLabelContentText())
{
LabelTTF* ltf = getLabelContentText();
ltf->setPosition(ccp(winSize.width / 2, winSize.height / 2));
ltf->setDimensions(CCSizeMake(contentSize.width - m_contentPadding * 2, contentSize.height - m_contentPaddingTop));
ltf->setHorizontalAlignment(kCCTextAlignmentLeft);
ltf->setColor(ccc3(0,0,0));
this->addChild(ltf);
}
}
}
............
}
//設置彩票號碼布局內容
void PopupLayer::setLotteryContext(Size size)
{
Size winSize = Director::getInstance()->getWinSize();
Size center =(winSize - size)/2;
//彩票布局行列為 10 X 3
for(int row=0; row<10; row++)
{
for(int col=0; col<3; col++)
{
//創建彩票號碼
LotteryCard* card = LotteryCard::createCardSprite((row+1)+ col*10, 40,40, center.width+20+row*(size.width/11), (winSize.height/2+30)⑷0*col);
//設置彩票的tag為彩票號碼
card->setTag((row+1)+ col*10);
//注冊彩票點擊回調函數refreshBallBackGround()
card->setBallCallbackFunc(this,callfuncN_selector(PopupLayer::refreshBallBackGround));
addChild(card);
//號碼放到彩票容器中
lotteryVector.pushBack(card);
//遍歷已選擇過的彩票容器,把已選過的置為不可見
for(int i=0;i<selected_number_vector.size();i++)
{
if(selected_number_vector.at(i) == (row+1)+ col*10)
{
card->setVisible(false);
}
}
}
}
}
//把選擇的號碼,放入已選擇過的彩票容器。參數_vector是表示已選擇過的,由角色調用時把角色自帶的lottery_vector傳來
void PopupLayer::setHasSelectedLotteryNumber(std::vector<int> _vector)
{
for(int i=0;i<_vector.size();i++)
{
selected_number_vector.push_back(_vector.at(i));
}
}
//更新號碼背景,并把選中的號碼做為tag傳給購買按鈕,由購買按鍵回傳給主場景
void PopupLayer::refreshBallBackGround(Node *pNode)
{
int tag2= pNode->getTag();
for(auto it=lotteryVector.begin();it!=lotteryVector.end();it++)
{
LotteryCard* node = (LotteryCard*)(*it);
int tag1= node->getTag();
if(node->getTag() != pNode->getTag())
{
node->setUnSelected();
}
}
lottery_selected = tag2;
Vector<Node*> menuItemVector = getMenuButton()->getChildren();
for(int i=0;i< getMenuButton()->getChildrenCount();i++)
{
if(menuItemVector.at(i)->getTag() != 0)
{
menuItemVector.at(i)->setTag(tag2);
break;
}
}
}
6、回到游戲主場景,看看是怎樣顯示出彩票對話框的
/注冊彩票消息的視察者
void GameBaseScene::registerNotificationObserver()
{
.............
NotificationCenter::getInstance()->addObserver(
this,
callfuncO_selector(GameBaseScene::receivedNotificationOMsg),
MSG_LOTTERY,
NULL);
}
//彩票消息處理
void GameBaseScene::receivedNotificationOMsg(Object* data)
{
..............
case MSG_LOTTERY_TAG:
{
int playerTag = messageVector.at(3)->intValue();
moveTag = messageVector.at(4)->intValue();
switch(playerTag)
{
case PLAYER_1_TAG:
{
//如果是第1角色,創建對話框,設置類型為彩票類型
PopupLayer* popDialogLottery = PopupLayer::create(DIALOG_BG);
popDialogLottery->setContentSize(CCSizeMake(Dialog_Size_Width, Dialog_Size_Height));
popDialogLottery->setTitle(LanguageString::getInstance()->getLanguageString(SELECT_LOTTERY_TITLE)->getCString());
popDialogLottery->setContentText("", 20, 60, 250);
popDialogLottery->setPopType(LOTTERY);
//添加回調函數lotteryButtonCallback ,當點擊購買或取消將調用該方法
popDialogLottery->setCallbackFunc(this, callfuncN_selector(GameBaseScene::lotteryButtonCallback));
//把角色已買過的號碼傳給對話框,讓其不可見
popDialogLottery->setHasSelectedLotteryNumber(player1->lottery_vector);
//添加2個按鍵,購買 取消
popDialogLottery->addButton(BUTTON_BG1, BUTTON_BG3, LanguageString::getInstance()->getLanguageString(BUY_OK)->getCString(), Btn_OK_TAG);
popDialogLottery->addButton(BUTTON_BG2, BUTTON_BG3, LanguageString::getInstance()->getLanguageString(CANCEL)->getCString(), Btn_Cancel_TAG);
this->addChild(popDialogLottery);
break;
}
case PLAYER_2_TAG:
{
//如果是角色2,則隨機購買彩票,不彈出對話框
int random_lottery_number = rand()%(30) + 1;
repeatForCheck:
for(int i=0;i<player2->lottery_vector.size();i++)
{
if(player2->lottery_vector.at(i) == random_lottery_number)
{
random_lottery_number = rand()%(30) + 1;
goto repeatForCheck;
}
}
//把購買后的彩票放到角色采票容器中
player2->lottery_vector.push_back(random_lottery_number);
//更新角色資金
refreshMoneyLabel(player2,-BUY_LOTTERY_MONEY);
//如果是角色行走終了,停留位置有彩票標志致使發送的彩票消息,則購買彩票終了后,需要發送處理角色上下左右相鄰地塊的消息
if(moveTag == GOEND)
{
CocosToast::createToast(this, String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(BUY_LOTTERY)->getCString(),BUY_LOTTERY_MONEY)->getCString(), TOAST_SHOW_TIME,player2->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGDealAroundLand2);
}
//如果是走完1步,就是途經彩票標示發送的彩票消息,則發送行走下1步的消息,繼續走下1步
else if(moveTag == MOVEPASS)
{
CocosToast::createToast(this, String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(BUY_LOTTERY)->getCString(),BUY_LOTTERY_MONEY)->getCString(), TOAST_SHOW_TIME,player2->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGMoveOneStep);
}
break;
}
}
break;
}
}
//發送走下1步的消息
void GameBaseScene::sendMSGMoveOneStep()
{
NotificationCenter::getInstance()->postNotification(MSG_MOVE_ONE_STEP,String::createWithFormat("%d",MSG_MOVE_ONE_STEP_TAG));
}
//對話框購買或取消按鍵點擊后的回調函數
void GameBaseScene::lotteryButtonCallback(Node *pNode)
{
//如果點擊的是購買按鍵
if(pNode->getTag() != ⑴ && pNode->getTag() != Btn_Cancel_TAG)
{
//更新角色1的彩票容器
player1->lottery_vector.push_back(pNode->getTag());
//更新資金
refreshMoneyLabel(player1,-BUY_LOTTERY_MONEY);
//如果是角色行走終了,停留位置有彩票標志致使發送的彩票消息,則購買彩票終了后,需要發送處理角色上下左右相鄰地塊的消息
if(moveTag == GOEND)
{
CocosToast::createToast(this, String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(BUY_LOTTERY)->getCString(),BUY_LOTTERY_MONEY)->getCString(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGDealAroundLand2);
}else if(moveTag == MOVEPASS)//如果是走完1步,就是途經彩票標示發送的彩票消息,則發送行走下1步的消息,繼續走下1步
{
CocosToast::createToast(this, String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(BUY_LOTTERY)->getCString(),BUY_LOTTERY_MONEY)->getCString(), TOAST_SHOW_TIME,player1->getPosition(),(SEL_CallFun)&GameBaseScene::sendMSGMoveOneStep);
}
pNode->getParent()->getParent()->removeFromParent();
}
else //處理取消按鍵
{
pNode->getParent()->getParent()->removeFromParent();
if(moveTag == GOEND)
{
sendMSGDealAroundLand2();
}else if(moveTag == MOVEPASS)
{
sendMSGMoveOneStep();
}
}
}
7、
在控制器中注冊走下1步的視察者
void RicherGameController::registerNotificationObserver()
{
.....
NotificationCenter::getInstance()->addObserver(
this,
callfuncO_selector(RicherGameController::receivedMsg),
MSG_MOVE_ONE_STEP,
NULL);
}
//收到走1不的消息后,調用moveOneStep ,繼續下1步的行走,至此購買彩票事件處理終了
void RicherGameController::receivedMsg(Object* data)
{
..............
if(retMsgType == MSG_MOVE_ONE_STEP_TAG)
{
moveOneStep(_richerPlayer);
}
}
點擊下載代碼
未完待續........................
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈