Cocos2d-x to achieve Android Toast function, cocos2d-xandroid

Source: Internet
Author: User

Cocos2d-x to achieve Android Toast function, cocos2d-xandroid
1. Toast

Android Toast is a View that quickly displays a small amount of information. It is mainly used for prompts and help. This article implements the most basic operations 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 functions, facilitating class direct call of 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. Use Code
Void HelloWorld: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = platform) | (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}




Android programming question. There is a button in the figure. You can click the button and use Toast to display your own name.

// Copy to the onCreate directory. The layout xml is not written. If necessary, you can write the layout by yourself and then use the find button to obtain the layout, currently, this is the entire avti.pdf is a buttonButton B = new Button (this); B. setText ("name"); B. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (MainActivity. this, "XXX", Toast. LENGTH_LONG ). show () ;}}); setContentView (B );

Small android game process with Cocos2d-x C ++ Engine

This is a bit complicated and you need to have android sdk, ndk, cocos2d-x package, eclipse.
First I use programming tools to write code that can run, I am a cocos2d-x program written in xcode on mac
Then create a work android project with the cocos2d-x (need to configure the android sdk and ndk), and then write your own cocos2d-x program class files put in the class directory of android, cocos2d-x resources put in the resource Directory of android, and then set the Good Luck line android program to compile the file, and then in the terminal bulid_native.sh to compile this file, if the compilation is successful, open this directory in eclipse to implement this small android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.