加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x实现Android的Toast功能

发布时间:2020-12-14 19:17:44 所属栏目:百科 来源:网络整理
导读: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;u

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,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,-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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读