Cocos2d-x實現Android的Toast功能,cocos2d-xandroid
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}
android編程題,圖中有一個按紐,功可以是點擊按鈕後,通過Toast顯示自己的名字試編程實現該功可以
//複製到onCreate下面,這個是沒有寫布局xml的,如果需要可以自己寫布局然後button用find去擷取,當前這個是整個avtivity就是一個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);
使用Cocos2d-x的C++引擎實現的小android遊戲的流程
這個有點複雜的,你需要有android的sdk,ndk,cocos2d-x包,eclipse。
首先自己用編程工具寫成可以啟動並執行代碼,我是在mac下用xcode寫的cocos2d-x程式
然後用cocos2d-x建立一個工android工程(需要配置好android的sdk和ndk),然後把自己寫的cocos2d-x程式的類檔案放到android的class目錄下,cocos2d-x的資源放到android的resource目錄下,然後設定好運行android程式要編譯的檔案,然後在終端bulid_native.sh編譯下這個檔案,如果編譯成功,再把這個目錄在eclipse下開啟,就可以實現這個小的android程式了。