Cocos2d-x:静态方法实现android的Toast效果
2016-04-09 11:20
471 查看
在cocos2dx中静态方法实现android 的Toast效果,代码如下:
调用方法
#include "ShowToast.h"
ShowToast::ShowText(this,"使用能量",3.0f);
<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, 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, 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 ShowToast::removeToast(Node* node) { log("node = %s",node->getDescription().c_str()); this->removeFromParentAndCleanup(true); }</span>
调用方法
#include "ShowToast.h"
ShowToast::ShowText(this,"使用能量",3.0f);
相关文章推荐
- 高级cocos2d-x招聘面试试题参考
- 【步兵 cocos-js】逆向思维 对 js对象和函数进行分析
- cocos2d-x 2.x打飞机(三)
- cocos2d-x 2.x打飞机(二)
- cocos2d-x 2.x打飞机(一)
- cocos2d-x 2.x粒子系统
- cocos2d-x 2.x 场景切换、事件处理
- cocos2d-x 2.x动作
- cocos2d-x 2.x动画、定时器、音乐音效
- cocos2d-x 3.x pageView的使用
- cocos2d-x 3.x 场景切换特效大集合
- 【复习笔记】 cocos2d-x 2.x 渲染特效实现 六 节点树的描边效果
- 敏感词过滤 DFA 状态机 cocos2d-js 实现版
- cocos2dx创建精灵的五种方法
- cocos2d-x 2.x01基础入门
- Cocos2d-x:编译Coco2d-x android 程序出现make: *** No rule to make target ...CC*.o'. Stop.
- Cocos2d-x 3.4在AndroidStudio上编译配置
- cocos2d-lua09apk打包发布&C++注册到Lua
- cocos2d-Lua08常用SocostudioUI组件及编辑器
- cocos2dx编译出错_最简单解决方法