Cocos2d-x实现android的Toast效果

浏览:
字体:
发布时间:2013-12-13 14:31:36
来源:

感觉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);

背景图/


<喎

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();