Cocos2d-x实现android的Toast效果
2013-12-12 11:19
405 查看
感觉android中的toast效果挺不错的,就试着自己用2dx做了一下,挺简单的,效果也不错。
XYToast.h
XYToast.cpp
调用方法
背景图
再用静态方法写一个,一样的效果,调用更加方便
Tools.h
调用方法
这样调用起来就方便多了,也实现了回调自身进行移除
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);
这样调用起来就方便多了,也实现了回调自身进行移除
相关文章推荐
- Cocos2d-x:静态方法实现android的Toast效果
- Cocos2d-x 3.0 实现android的Toast效果
- android实现类似toast效果的圆角dialog警告框
- Cocos2d-x实现Android的Toast功能
- 网页中android Toast效果的实现
- 微信小程序开发常用技巧(7)——实现一个类似于Android toast效果动画
- 网页中android Toast效果的实现
- iOS上实现Android的Toast效果
- Android--强大的Toast实现多种效果
- js实现动态消息提示框(模仿android系统中toast的效果)
- 【Android常用控件】Toast的几种效果实现(附源码)
- Cocos2d-x实现Android的Toast特征
- Android Toast实现提示效果
- Android--强大的Toast实现多种效果
- 【Android常用控件】Toast的几种效果实现(附源码)
- Cocos2d-x学习之实现类似android的菜单栏tabbar效果
- android从源码解析并实现各种Toast效果合集
- Android应用系列:仿MIUI的Toast动画效果实现(有图有源码)
- android从源码解析并实现各种Toast效果合集
- Cocos2d-x实现Android的Toast功能