#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);
}
2016年5月20日 星期五
0520 cocos2d-x 3.0 TOAST 範例
http://www.it165.net/pro/html/201411/25654.html
1、Toast
Android的Toast是一個View視圖,快速為用戶顯示少量的信息。主要用於一些提示和幫助。本文實現了Toast最基本的操作能,代碼如下
//PacToast.h
#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
class PacToast : public LayerColor
{
public:
static void makeText(Node* node,const std::string& msg,const float& time);//靜態函數,方便類直接調用
void removeToast(Node* node);
};
//PacToast
#include "PacToast.h"
void PacToast::makeText(cocos2d::Node *node, const std::string &msg, const float &time)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto pLabel = Label::createWithSystemFont(msg.c_str(), "Arial", 30);
pLabel->setColor(Color3B::WHITE);
pLabel->ignoreAnchorPointForPosition(false);
pLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
auto ly = LayerColor::create(Color4B(130,120,120,255));
ly->ignoreAnchorPointForPosition(false);
ly->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
ly->setContentSize(pLabel->getContentSize() + Size(20,15));
node->addChild(ly);
node->addChild(pLabel);
ly->setPosition(Vec2(visibleSize.width/2,-pLabel->getContentSize().height));
pLabel->setPosition(ly->getPosition());
auto seq1 = Sequence::create(FadeIn::create(time/5), DelayTime::create(time/5*1.5),FadeOut::create(time/5*2.5),CallFuncN::create(ly,callfuncN_selector(PacToast::removeToast)),nullptr);
auto seq2 = Sequence::create(EaseSineIn::create(MoveBy::create(time/5, Vec2(0,200))),DelayTime::create(time/5*2),EaseSineOut::create(MoveBy::create(time/3, Vec2(0,-200))), nullptr);
auto spawn = Spawn::create(seq1, seq2, nullptr);
auto action = Repeat::create(spawn,1);
ly->setOpacity(0);
pLabel->setOpacity(0);
ly->runAction(action);
pLabel->runAction(action->clone());
}
void PacToast::removeToast(Node* node)
{
log("node = %s",node->getDescription().c_str());
this->removeFromParentAndCleanup(true);
}
2、使用代碼
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
PacToast::makeText(this, "我是Toast!", 2.5f);
#endif
訂閱:
文章 (Atom)
cocos2dx-lua 建立滑鼠監聽
重要關鍵字 EVENT_MOUSE_SCROLL addEventListenerWithSceneGraphPriority if IsPc() then --建立滑鼠監聽 local listener = cc.EventListenerMouse...
-
https://changeyu.wordpress.com/2015/03/25/%E4%BD%A0%E7%9A%84macbook-air%E7%B8%BD%E6%98%AF%E7%A9%BA%E9%96%93%E4%B8%8D%E5%A4%A0%E5%97%8E%EF%BC...
-
http://kirkhsutw.blogspot.tw/2015/05/ios-ad-hoc.html [iOS] 第一次 Ad Hoc 就上手 之前都沒想過要用這個 Ad Hoc,這一次是因為用到了Dropbox API,在Submit被re...
-
答案:直接回傳 std :: vector < MyObj > myFunction (); C++: What is the best method to return a vector of objects from a function...