Cocos2d-x实现android的Toast效果

感觉android中的toast效果挺不错的,就试着自己用2dx做了一下,挺简单的,效果也不错。

XYToast.h

#pragma once

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace std;

class XYToast: public CCLayerColor
{
public:
	CCSize visibleSize;
	CCPoint origin;
	CCScale9Sprite* bg;
public:
    XYToast();
    ~XYToast();
    void removeSelf();
    virtual bool init();
	void initToast(string msg,float time);
	CREATE_FUNC(XYToast);
    void onExit();
};

XYToast.cpp

#include "XYToast.h"

XYToast::XYToast()
{
}

XYToast::~XYToast()
{
}

bool XYToast::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 0)));//ccc4(0, 0, 0, 125)
		visibleSize = CCDirector::sharedDirector()->getVisibleSize();
		origin = CCDirector::sharedDirector()->getVisibleOrigin();
		//====================================
		bg = CCScale9Sprite::create("toast_bg.png");
		bg->setPosition(ccp(origin.x+visibleSize.width/2,origin.y+130*visibleSize.height/960));
		//============================
        bRet = true;
    } while (0);
    return bRet;
}

void XYToast::onExit()
{
    CCLayerColor::onExit();
}

void XYToast::initToast( string msg,float time )
{
	CCLabelTTF* pLabel = CCLabelTTF::create(msg.c_str(), "Arial", 21);
	pLabel->setColor(ccWHITE);
	bg->setContentSize(CCSizeMake(pLabel->getContentSize().width+10,pLabel->getContentSize().height+10));
	bg->addChild(pLabel, 1);
	this->addChild(bg,10);
	pLabel->setPosition(ccp(bg->getContentSize().width/2,bg->getContentSize().height/2));
	pLabel->runAction(CCSequence::create(CCFadeIn::create(time/5),
		CCDelayTime::create(time/5*3),CCFadeOut::create(time/5),NULL));
	bg->runAction(CCSequence::create(CCFadeIn::create(time/5),
		CCDelayTime::create(time/5*3),CCFadeOut::create(time/5),
		CCCallFunc::create(this,callfunc_selector(XYToast::removeSelf)),NULL));
	//=========================
	//this->scheduleOnce(schedule_selector(XYToast::removeSelf),time);
}

void XYToast::removeSelf()
{
	this->removeFromParentAndCleanup(true);
}

调用方法

	XYToast* toast = XYToast::create();
	toast->initToast(Tools::a2u("登录成功").c_str(),2.5f);
	this->addChild(toast,10);

背景图


再用静态方法写一个,一样的效果,调用更加方便

Tools.h

	static void showToast(CCNode* node,string msg,float dt);
	void removeToast(CCNode* node);
Tools.cpp

void Tools::showToast(CCNode* node,string msg,float dt )
{
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCScale9Sprite* bg = CCScale9Sprite::create("toast_bg.png");
	bg->setPosition(ccp(origin.x+visibleSize.width/2,origin.y+130*visibleSize.height/960));
	CCLabelTTF* pLabel = CCLabelTTF::create(msg.c_str(), "Arial", 21);
	pLabel->setColor(ccWHITE);
	bg->setContentSize(CCSizeMake(pLabel->getContentSize().width+10,pLabel->getContentSize().height+10));
	bg->addChild(pLabel, 1);
	node->addChild(bg,10);
	pLabel->setPosition(ccp(bg->getContentSize().width/2,bg->getContentSize().height/2));
	pLabel->runAction(CCSequence::create(CCFadeIn::create(dt/5),
		CCDelayTime::create(dt/5*3),CCFadeOut::create(dt/5),NULL));
	bg->runAction(CCSequence::create(CCFadeIn::create(dt/5),
		CCDelayTime::create(dt/5*3),CCFadeOut::create(dt/5),
		CCCallFuncN::create(bg,callfuncN_selector(Tools::removeToast)),
		NULL));
}

void Tools::removeToast(CCNode* node)
{
	node->removeFromParentAndCleanup(true);
}

调用方法

Tools::showToast(this,"error",3.0f);

这样调用起来就方便多了,也实现了回调自身进行移除



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值