2016年5月20日 星期五

0520 toast 範例2

#include "cocos2d.h"
using namespace cocos2d;

USING_NS_CC;
using namespace std;

class Toast: public LayerColor
{
public:
    Size winSize;
    Vec2 winOrigin;
// CCSize visibleSize;
// CCPoint origin;
//CCScale9Sprite* bg;
Sprite *bg;
public:
Toast();
~Toast();
void removeSelf();
virtual bool init();
void initToast(string msg,float time);
CREATE_FUNC(Toast);
void onExit();
};


//////////////////////////////////////////////////////////////////////////
//建立通用彈跳視窗
//參考http://www.android100.net/html/201406/04/18200.html
Toast::Toast()
{
}

Toast::~Toast()
{
}

bool Toast::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!LayerColor::initWithColor(Color4B(0, 0, 0, 0)));//ccc4(0, 0, 0, 125)
winSize = Director::getInstance()->getVisibleSize();
winOrigin = Director::getInstance()->getVisibleOrigin();
//====================================
bg = Sprite::createWithSpriteFrameName(S_MSGBOX_BK);
//bg = CCScale9Sprite::createWithSpriteFrameName(S_BTN_BACKGROUNP);
bg->setPosition(Vec2(winOrigin.x+winSize.width/2,winOrigin.y+130*winSize.height/1080));
//============================
bRet = true;
} while (0);
return bRet;
}

void Toast::onExit()
{
LayerColor::onExit();
}

void Toast::initToast( string msg,float time )
{
auto pLabel = Label::createWithSystemFont(msg.c_str(), S_FONE, *pSTR_TOAST_SIZE);
pLabel->setColor(COLOR_STRING_BLACK);
Size tSizeOrig=bg->getContentSize();
Size tTargetSize=pLabel->getContentSize();
//bg->setContentSize(CCSizeMake(pLabel->getContentSize().width+10,pLabel->getContentSize().height+10));
bg->setScaleX( ( tTargetSize.width / tSizeOrig.width )); //利用CCSPRITE 需額外進行縮放
bg->setScaleY( ( tTargetSize.height / tSizeOrig.height)); //利用CCSPRITE 需額外進行縮放
bg->setColor(Color3B(240,240,240));
bg->addChild(pLabel, 1);
this->addChild(bg,10);
pLabel->setPosition(bg->getContentSize()/2);
pLabel->setScaleX( ( tSizeOrig.width /tTargetSize.width  )); //利用CCSPRITE 需額外進行縮放
pLabel->setScaleY( ( tSizeOrig.height/ tTargetSize.height )); //利用CCSPRITE 需額外進行縮放
pLabel->runAction(Sequence::create(FadeIn::create(time/5),
DelayTime::create(time/5*3),FadeOut::create(time/5),NULL));
bg->runAction(Sequence::create(FadeIn::create(time/5),
DelayTime::create(time/5*3),FadeOut::create(time/5),
//CallFunc::create(this,callfunc_selector(Toast::removeSelf)),NULL));
        CallFunc::create(CC_CALLBACK_0(Toast::removeSelf, this)),NULL));

//=========================
//this->scheduleOnce(schedule_selector(XYToast::removeSelf),time);
}

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

沒有留言:

張貼留言

cocos2dx-lua 建立滑鼠監聽

重要關鍵字  EVENT_MOUSE_SCROLL addEventListenerWithSceneGraphPriority      if IsPc() then --建立滑鼠監聽         local listener = cc.EventListenerMouse...