Cocos2d-x 3.2 大富翁游戏项目开发-第十七部分 Toast提示框

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第十七部分 Toast提示框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在缴纳过路费时,在角色附近显示减少的资金数,收到过路费的角色显示增加的资金数,效果如图



网上有不少这方面的资料,我拿来稍微修改了一下

写一个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,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,-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)
{
}

现在修改一下GameBaseScenepayTolls方法


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",player1->getPosition());
   //注意当是第一角色时,延时TOAST_SHOW_TIME秒后发送继续行走消息,避免toast显示过于频繁
   scheduleOnce(schedule_selector( GameBaseScene::sendMSGPickOneToGO),TOAST_SHOW_TIME);	
   break;
  }
  case PLAYER_2_TAG: 
  {
    int retMoney = displayArea(x,player2,player1_building_1_tiledID,player1_building_2_tiledID,player1_building_3_tiledID);
    refreshMoneyLabel(landOwner,money + retMoney);
    refreshMoneyLabel(player2,-(money + retMoney));
    CocosToast::createToast(this,player2->getPosition());
    NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String::createWithFormat("%d",MSG_PICKONE_TOGO_TAG));
    break;
  }				
}

……………….

}

点击下载代码


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


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

原文链接:https://www.f2er.com/cocos2dx/345060.html

猜你在找的Cocos2d-x相关文章