With the default Cocos2dx template the back button on your phone won't close the application. Without this your app will fail the test procedure for some curated stores (eg Samsung). Here's how I did it.
Firstly, you need to decide how you want to display a 'Do you want to quit' type money. There are three options I guess, showing an extra menu on your 'HelloWorld' scene, calling a native java dialog, or showing a 'Quit' layer. I decided on the latter as it would be easy to reuse the code. So I made a very basic called call 'QuitLayer' as follows:
Here's the header:#ifndef __QUIT_SCENE_H__ #define __QUIT_SCENE_H__ #include "cocos2d.h" class QuitLayer : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); void goQuit(CCObject* pSender); void goBack(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(QuitLayer); }; #endif
Here's the class. There's nothing very clever here - it uses graphics called 'quitdialog.png' 'butyes.png' etc to construct the dialog box.
If you click 'Yes' (to quit) it calls:
CCDirector::sharedDirector()->end();
Which ends the app.
#include "QuitLayer.h" #include "SimpleAudioEngine.h" #include "HelloWorldScene.h" using namespace cocos2d; using namespace CocosDenshion; static float scale = 0.5; static float screenWidth; static float screenHeight; CCScene* QuitLayer::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object QuitLayer *layer = QuitLayer::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool QuitLayer::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); screenWidth = size.width; screenHeight = size.height; pSprite = CCSprite::create("introback.png"); pSprite->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(pSprite, 0); CCSprite * quitBox = CCSprite::create("quitdialog.png"); quitBox->setScale(scale); quitBox->setPosition(ccp(screenWidth/2,screenHeight/2)); this->addChild(quitBox,2000); int fontsize = 64*scale; // Button menu CCMenuItem * butYes= CCMenuItemImage::create("butyes.png" ,"butyespressed.png" ,this, menu_selector(QuitLayer::goQuit) ); CCMenuItem * butNo= CCMenuItemImage::create("butno.png" ,"butnopressed.png" ,this, menu_selector(QuitLayer::goBack)); CCMenu *Menu = CCMenu::create(butYes,butNo,NULL); Menu->setPosition(ccp(screenWidth/2, screenHeight/2)); butYes->setPosition(ccp(-15,0)); butNo->setPosition(ccp(150,0)); this->addChild(Menu,3000); return true; } void QuitLayer::goQuit(CCObject* pSender) { CCDirector::sharedDirector()->end(); } void QuitLayer::goBack(CCObject* pSender) { CCScene *pScene = HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(pScene); }
You'll now need to call this from 'HelloWorld' (or whatever you called your first scene) when the back button is pressed. That's actually very easy.
Add a method called 'keyBackClicked' to call your Quit scene when the button is pressed.
To HelloWorldScene.h add
void keyBackClicked();
To HelloWorldScene add.
void HelloWorld::keyBackClicked() { CCScene *pScene = QuitLayer::scene(); CCDirector::sharedDirector()->replaceScene(pScene); }
Finally, you need to enable the keypad. So put something like this in your init in HelloWorld:
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) this->setKeypadEnabled(true); #endif
Thank you for your very nice article, do not forget to read my articles also
ReplyDeletegambar lucu
kata kata cinta
kata kata galau
kata kata lucu
kata kata mutiara
dp bbm cinta
cara menghilangkan jerawat
kata kata mutiara cinta
are deliberately presented to the loyal readers.