微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐