Cocos2d-x实现Android的Toast特征
2015-08-17 18:42
381 查看
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 }
相关文章推荐
- cocos2d-x学习笔记(二)序列帧动画实现的几种方法
- (转载)我所理解Cocos2d-x 3.6(Lua):Cocos如何绑定Lua自定义类
- 14、Cocos2dx 3.0三,找一个小游戏开发Scene and Layer:游戏梦想
- cocos2dx-3.x lua绑定
- cocos2d-x在Android真机上使用Sqlite数据库
- cocos自定义类 绑定到lua
- Cocos2dx播放mp4文件(IOS和Android)
- COCOS2D-JS的屏幕适配方案
- CocoStudio:ImageView分析
- [转]cocos2dx中用外部浏览器打开url
- Cocos2d-x能够实现的效果总结
- Agar 流体效果的 Cocos2d-JS 实现
- Cocos2dx_背景层循环滚动
- 在cocos2d-x在CCTableView使用控制
- 9秒学院总结Cocos2d-X面试题
- Eclipse编译调试Cocos2d问题汇总
- Cocos2dx_视觉差效果层ParallaxNode
- 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第三节:3D摄像机
- 9秒学院总结Cocos2d-X面试题
- Cocos2d-x问题纪录:removeChild和附有PhysicsBody的csb动画