1. Toast
The Android toast is a view that quickly displays a small amount of information to the user. Mainly used for some hints and help. This paper realizes the most basic operation of toast,
The code is as follows:
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);//static function, convenient class directly call 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. Using Code
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, "I am toast! ", 2.5f); #endif}
Cocos2d-x implementing the Toast feature of Android