Cocos2d-x 3.2 大富翁遊戲項目開發-第十七部分 Toast提示框,cocos2d-xtoast
在繳納過路費時,在角色附近顯示減少的資金數,收到過路費的角色顯示增加的資金數,效果
網上有不少這方面的資料,我拿來稍微修改了一下
寫一個CocosToast多士類
#ifndef __CocosToast_H__#define __CocosToast_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;using namespace std;class CocosToast :public LayerColor{public:CocosToast(void);~CocosToast(void);static void createToast(Node* node,const std::string& msg,const float& time,Vec2 point);void removeToast(Node* node);};#endif
#include "CocosToast.h"參數node:添加該Toast layer的父節點msg:顯示的資訊time:toast顯示的時間長短point:toast顯示的位置座標void CocosToast::createToast(cocos2d::Node *node, const std::string &msg, const float &time,Vec2 point){ //建立顯示資訊的label auto label = Label::createWithSystemFont(msg.c_str(), "Arial", 20); label->setColor(Color3B::WHITE); label->ignoreAnchorPointForPosition(false); label->setAnchorPoint(Vec2::ANCHOR_MIDDLE); //toast的layer層 auto layer = LayerColor::create(Color4B(100,100,100,255)); layer->ignoreAnchorPointForPosition(false); layer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); layer->setContentSize(label->getContentSize() + Size(20,15)); node->addChild(layer); node->addChild(label); layer->setPosition(point); label->setPosition(layer->getPosition()); //toast顯示時的動作,先由下而上,再由上而下,回到point的位置,動作結束時,把toast從父節點清除 auto seq1 = Sequence::create(FadeIn::create(time/5), DelayTime::create(time/5*1.5),FadeOut::create(time/5*2.5),CallFuncN::create(layer,callfuncN_selector(CocosToast::removeToast)),NULL); auto seq2 = Sequence::create(EaseSineIn::create(MoveBy::create(time/5, Vec2(0,50))),DelayTime::create(time/5*2),EaseSineOut::create(MoveBy::create(time/3, Vec2(0,-50))), NULL); auto spawn = Spawn::create(seq1, seq2, NULL); auto action = Repeat::create(spawn,1); layer->setOpacity(0); label->setOpacity(0); layer->runAction(action); label->runAction(action->clone());}void CocosToast::removeToast(Node* node){ this->removeFromParentAndCleanup(true);}CocosToast::CocosToast(void){}CocosToast::~CocosToast(void){}
現在修改一下GameBaseScene的payTolls方法
void GameBaseScene::payTolls(int payTag,float x,float y ,int playerTag){…………switch(playerTag){ case PLAYER_1_TAG: { int retMoney = displayArea(x,y,player1,player2_building_1_tiledID,player2_building_2_tiledID,player2_building_3_tiledID); refreshMoneyLabel(landOwner,money + retMoney); refreshMoneyLabel(player1,-(money + retMoney)); //Toast顯示相應角色增加和減少的資金數 CocosToast::createToast(this, String::createWithFormat("+%d",money + retMoney)->getCString(), TOAST_SHOW_TIME,landOwner->getPosition()); CocosToast::createToast(this, String::createWithFormat("-%d",money + retMoney)->getCString(), TOAST_SHOW_TIME,player1->getPosition()); //注意當是第一角色時,延時TOAST_SHOW_TIME秒後發送繼續行走訊息,避免toast顯示過於頻繁 scheduleOnce(schedule_selector( GameBaseScene::sendMSGPickOneToGO),TOAST_SHOW_TIME); break; } case PLAYER_2_TAG: { int retMoney = displayArea(x,y,player2,player1_building_1_tiledID,player1_building_2_tiledID,player1_building_3_tiledID); refreshMoneyLabel(landOwner,money + retMoney); refreshMoneyLabel(player2,-(money + retMoney)); CocosToast::createToast(this, String::createWithFormat("+%d",money + retMoney)->getCString(), TOAST_SHOW_TIME,landOwner->getPosition()); CocosToast::createToast(this, String::createWithFormat("-%d",money + retMoney)->getCString(), TOAST_SHOW_TIME,player2->getPosition()); NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG)); break; }}……………….}
點擊下載代碼
http://download.csdn.net/detail/lideguo1979/8334883
未完待續...................