Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Cocos2d-x實現android的Toast效果

Cocos2d-x實現android的Toast效果

編輯:關於Android編程

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

背景圖\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+1NnTw76yzKy3vbeo0LTSu7j2o6zSu9H5tcTQp7n7o6y199PDuPy807e9seM8L3A+CjxwPlRvb2xzLmg8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;"> static void showToast(CCLayer*layer,string msg,float dt); void removeToast(CCNode* node);Tools.cpp

void Tools::showToast(CCLayer*layer, 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);
	layer->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);

這樣調用起來就方便多了,也實現了回調自身進行移除



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved