Cocos2d-x:静态方法实现android的Toast效果

在cocos2dx中静态方法实现android 的Toast效果代码如下:


<span style="font-size:14px;">//
//  ShowToast.h
//  BananaCard
//
//  Created by Alostz on 16/4/8.
//  toast方法
//

#ifndef ShowToast_h
#define ShowToast_h

#include "cocos2d.h"
#include "ui/CocosGUI.h"

using namespace cocos2d;
using namespace std;

class ShowToast : public LayerColor
{
public:
    static void ShowText(Node* node,const string& msg,const float& time); //静态函数,方便类直接调用
    void removeToast(Node* node);
};

#endif /* ShowToast_h */
</span>

<span style="font-size:14px;">//
//  ShowToast.cpp
//  BananaCard
//
//  Created by Alostz on 16/4/8.
//
//

#include <ShowToast.h>

void ShowToast::ShowText(cocos2d::Node *node,const string &msg,const float &time){
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    auto pLabel = Label::createWithSystemFont(msg.c_str(),"Arial",14);
    pLabel->setColor(Color3B::WHITE);
    pLabel->ignoreAnchorPointForPosition(false);
    pLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    
    auto ly = LayerColor::create(Color4B(130,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,CC_CALLFUNCN_SELECTOR(ShowToast::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,-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 ShowToast::removeToast(Node* node)
{
    log("node = %s",node->getDescription().c_str());
    this->removeFromParentAndCleanup(true);
}</span>

调用方法


#include "ShowToast.h"


ShowToast::ShowText(this,"使用能量",3.0f);

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...