Cocos2d-x學習筆記(19)(TestCpp源碼分析-3)

來源:互聯網
上載者:User

本章主要介紹testBasic.h/cpp,這兩個檔案主要用於返回主情境介面。

//testBasic.h#ifndef _TEST_BASIC_H_#define _TEST_BASIC_H_#include "cocos2d.h"#include "VisibleRect.h"USING_NS_CC;using namespace std;class TestScene : public CCScene  //繼承自CCScene{public:     TestScene(bool bPortrait = false);  //建構函式    virtual void onEnter();  //重寫onEnter函數    virtual void runThisTest() = 0;  //重寫runThisTest函數    // The CallBack for back to the main menu scene    virtual void MainMenuCallback(CCObject* pSender);  //返回主菜單函數};typedef CCLayer* (*NEWTESTFUNC)();  //NEWTESTFUNC表示一個傳回型別為CCLayer*,沒有參數的函數指標#define TESTLAYER_CREATE_FUNC(className) \  //TESTLAYER_CREATE_FUNC表示一個建立CCLayer的宏函數static CCLayer* create##className() \  //用兩個##來在宏中替換className為宏定義的參數{ return new className(); }#define CF(className) create##className  //CF表示 createXXX;比如CF(Scene)表示:createScene#endif
以下是testBasic.cpp檔案介紹

//testBasic.cpp#include "testBasic.h"#include "controller.h"TestScene::TestScene(bool bPortrait)  //建構函式{        CCScene::init();  //調用基類的初始化函數}void TestScene::onEnter(){    CCScene::onEnter();  //調用基類的onEnter函數    //add the menu item for back to main menu//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)//    CCLabelBMFont* label = CCLabelBMFont::create("MainMenu",  "fonts/arial16.fnt");//#else    CCLabelTTF* label = CCLabelTTF::create("MainMenu", "Arial", 20);//#endif  //將label與返回函數MainMenuCallback相關聯    CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestScene::MainMenuCallback));    CCMenu* pMenu =CCMenu::create(pMenuItem, NULL);  ///用pMenuItem初始化CCMenu對象    pMenu->setPosition( CCPointZero );  //設定pMenu位置    pMenuItem->setPosition( ccp( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) ); //設定返回按鍵位置    addChild(pMenu, 1);}void TestScene::MainMenuCallback(CCObject* pSender)  //返回主菜單函數{    CCScene* pScene = CCScene::create();  //建立新情境    CCLayer* pLayer = new TestController();  //建立TestController層    pLayer->autorelease();    pScene->addChild(pLayer);  //將新層加入到新情境    CCDirector::sharedDirector()->replaceScene(pScene);  //切換情境}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.