好的引擎,会提供一系列完整的功能示例,Cocos2d-x之所以能得到很多人的喜爱,其重要的原因是它提供了丰富而易学的示例。在cocos2d-2.0-x-2.0.2中这些示例被放在一个名叫TestCpp的工程中,为了更好的学习Cocos2d-x的功能示例,我们今天来学习一下这个工程的框架结构。
在VS的解决方案里展开TestCpp工程,其下有43个示例目录,除此之前还有几个文件:
AppDelegate.h/cpp : 程序控制类AppDelegate 。
controller.h/cpp:示例场景管理类TestController,用于显示所有示例的菜单。
testBasic.h/cpp:示例场景基类TestScene,用于返回到主界面场景。
testResource.h:文件资源名称字符串定义头文件
tests.h:示例总头文件
main.h/cpp:主函数及头文件
所有的示例都是写在单独的由TestScene派生场景类中,在这些场景中加入一些由CCLayer派生的示例对象来实现相应功能的展示。
与HelloWorld一样,我们需要在main.cpp中创建AppDelegate实例,并设置窗口大小,启动游戏程序。
- #include "main.h"
- #include "AppDelegate.h"
- #include "CCEGLView.h"
- USING_NS_CC;
-
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
-
- AppDelegate app;
-
- CCEGLView* eglView = CCEGLView::sharedOpenGLView();
- eglView->setFrameSize(480, 320);
-
- return CCApplication::sharedApplication()->run();
- }
在AppDelegate.cpp中的boolAppDelegate::applicationDid。FinishLaunching()函数中创建场景并运行场景
- bool AppDelegate::applicationDidFinishLaunching()
- {
-
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
-
- TargetPlatform target = getTargetPlatform();
-
- if (target == kTargetIpad)
- {
-
- if (pDirector->enableRetinaDisplay(true))
- {
- CCFileUtils::sharedFileUtils()->setResourceDirectory("ipadhd");
- }
- else
- {
- CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad");
- }
- }
-
- else if (target == kTargetIphone)
- {
-
- if (pDirector->enableRetinaDisplay(true))
- {
- CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");
- }
- }
-
-
- pDirector->setDisplayStats(true);
-
-
- pDirector->setAnimationInterval(1.0 / 60);
-
- CCScene * pScene = CCScene::create();
-
- CCLayer * pLayer = new TestController();
-
- pLayer->autorelease();
-
- pScene->addChild(pLayer);
-
- pDirector->runWithScene(pScene);
-
- return true;
- }
下面我们来看一下testBase.h/cpp,这里面有一个所有示例都要用到的场景基类TestScene。
- #ifndef _TEST_BASIC_H_
- #define _TEST_BASIC_H_
-
- #include "cocos2d.h"
-
- USING_NS_CC;
- using namespace std;
-
- class TestScene : public CCScene
- {
- public:
-
- TestScene(bool bPortrait = false);
-
- virtual void onEnter();
-
- virtual void runThisTest() = 0;
-
- virtual void MainMenuCallback(CCObject* pSender);
- };
-
- #endif
cpp文件:
- #include "testBasic.h"
- #include "controller.h"
-
- TestScene::TestScene(bool bPortrait)
- {
-
- CCScene::init();
- }
-
- void TestScene::onEnter()
- {
- CCScene::onEnter();
-
-
-
-
-
- CCLabelTTF* label = CCLabelTTF::create("MainMenu", "Arial", 20);
-
-
- CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestScene::MainMenuCallback));
-
- CCMenu* pMenu =CCMenu::create(pMenuItem, NULL);
- CCSize s = CCDirector::sharedDirector()->getWinSize();
- pMenu->setPosition( CCPointZero );
- pMenuItem->setPosition( CCPointMake( s.width - 50, 25) );
-
- addChild(pMenu, 1);
- }
-
- void TestScene::MainMenuCallback(CCObject* pSender)
- {
-
- CCScene* pScene = CCScene::create();
-
- CCLayer* pLayer = new TestController();
- pLayer->autorelease();
-
- pScene->addChild(pLayer);
- CCDirector::sharedDirector()->replaceScene(pScene);
- }
在TestScene中,提供了一个可以被点击的标签MainMenu,从字面意思就知道点击它可以返回主菜单界面,这个主菜单在哪呢?就是TestController。
- #ifndef _CONTROLLER_H_
- #define _CONTROLLER_H_
-
- #include "cocos2d.h"
-
- USING_NS_CC;
-
- class TestController : public CCLayer
- {
- public:
-
- TestController();
-
- ~TestController();
-
- void menuCallback(CCObject * pSender);
-
- void closeCallback(CCObject * pSender);
-
- virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
-
- virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
-
- private:
-
- CCPoint m_tBeginPos;
-
- CCMenu* m_pItemMenu;
- };
-
- #endif
进入Cpp看一下:
- #include "controller.h"
- #include "testResource.h"
- #include "tests.h"
-
- #define LINE_SPACE 40
-
- static CCPoint s_tCurPos = CCPointZero;
-
- static TestScene* CreateTestScene(int nIdx)
- {
-
- CCDirector::sharedDirector()->purgeCachedData();
-
-
- TestScene* pScene = NULL;
-
- switch (nIdx)
- {
- case TEST_ACTIONS:
- pScene = new ActionsTestScene(); break;
- case TEST_TRANSITIONS:
- pScene = new TransitionsTestScene(); break;
- case TEST_PROGRESS_ACTIONS:
- pScene = new ProgressActionsTestScene(); break;
- case TEST_EFFECTS:
- pScene = new EffectTestScene(); break;
- case TEST_CLICK_AND_MOVE:
- pScene = new ClickAndMoveTestScene(); break;
- case TEST_ROTATE_WORLD:
- pScene = new RotateWorldTestScene(); break;
- case TEST_PARTICLE:
- pScene = new ParticleTestScene(); break;
- case TEST_EASE_ACTIONS:
- pScene = new ActionsEaseTestScene(); break;
- case TEST_MOTION_STREAK:
- pScene = new MotionStreakTestScene(); break;
- case TEST_DRAW_PRIMITIVES:
- pScene = new DrawPrimitivesTestScene(); break;
- case TEST_COCOSNODE:
- pScene = new CocosNodeTestScene(); break;
- case TEST_TOUCHES:
- pScene = new PongScene(); break;
- case TEST_MENU:
- pScene = new MenuTestScene(); break;
- case TEST_ACTION_MANAGER:
- pScene = new ActionManagerTestScene(); break;
- case TEST_LAYER:
- pScene = new LayerTestScene(); break;
- case TEST_SCENE:
- pScene = new SceneTestScene(); break;
- case TEST_PARALLAX:
- pScene = new ParallaxTestScene(); break;
- case TEST_TILE_MAP:
- pScene = new TileMapTestScene(); break;
- case TEST_INTERVAL:
- pScene = new IntervalTestScene(); break;
- case TEST_CHIPMUNKACCELTOUCH:
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
- pScene = new ChipmunkAccelTouchTestScene(); break;
- #else
- #ifdef MARMALADEUSECHIPMUNK
- #if (MARMALADEUSECHIPMUNK == 1)
- pScene = new ChipmunkAccelTouchTestScene();
- #endif
- break;
- #endif
- #endif
- case TEST_LABEL:
- pScene = new AtlasTestScene(); break;
- case TEST_TEXT_INPUT:
- pScene = new TextInputTestScene(); break;
- case TEST_SPRITE:
- pScene = new SpriteTestScene(); break;
- case TEST_SCHEDULER:
- pScene = new SchedulerTestScene(); break;
- case TEST_RENDERTEXTURE:
- pScene = new RenderTextureScene(); break;
- case TEST_TEXTURE2D:
- pScene = new TextureTestScene(); break;
- case TEST_BOX2D:
- pScene = new Box2DTestScene(); break;
- case TEST_BOX2DBED:
- pScene = new Box2dTestBedScene(); break;
- case TEST_EFFECT_ADVANCE:
- pScene = new EffectAdvanceScene(); break;
- case TEST_ACCELEROMRTER:
- pScene = new AccelerometerTestScene(); break;
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
- case TEST_KEYPAD:
- pScene = new KeypadTestScene(); break;
- #endif
- case TEST_COCOSDENSHION:
- pScene = new CocosDenshionTestScene(); break;
- case TEST_PERFORMANCE:
- pScene = new PerformanceTestScene(); break;
- case TEST_ZWOPTEX:
- pScene = new ZwoptexTestScene(); break;
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
-
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
- case TEST_CURL:
- pScene = new CurlTestScene(); break;
- #endif
- #endif
- case TEST_USERDEFAULT:
- pScene = new UserDefaultTestScene(); break;
- case TEST_BUGS:
- pScene = new BugsTestScene(); break;
- case TEST_FONTS:
- pScene = new FontTestScene(); break;
- case TEST_CURRENT_LANGUAGE:
- pScene = new CurrentLanguageTestScene(); break;
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
- case TEST_TEXTURECACHE: pScene = new TextureCacheTestScene(); break;
- #endif
- case TEST_EXTENSIONS:
- {
- pScene = new ExtensionsTestScene();
- }
- break;
- case TEST_SHADER:
- pScene = new ShaderTestScene();
- break;
- case TEST_MUTITOUCH:
- pScene = new MutiTouchTestScene();
- break;
- default:
- break;
- }
-
- return pScene;
- }
-
- TestController::TestController()
- : m_tBeginPos(CCPointZero)
- {
-
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
-
- CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);
- CCSize s = CCDirector::sharedDirector()->getWinSize();
-
- pMenu->setPosition( CCPointZero );
- pCloseItem->setPosition(CCPointMake( s.width - 30, s.height - 30));
-
-
- m_pItemMenu = CCMenu::create();
-
- for (int i = 0; i < TESTS_COUNT; ++i)
- {
-
-
-
- CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24);
-
- CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));
-
- m_pItemMenu->addChild(pMenuItem, i + 10000);
-
- pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));
- }
-
- m_pItemMenu->setContentSize(CCSizeMake(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)));
-
- m_pItemMenu->setPosition(s_tCurPos);
-
- addChild(m_pItemMenu);
-
- setTouchEnabled(true);
-
- addChild(pMenu, 1);
-
- }
-
- TestController::~TestController()
- {
- }
-
- void TestController::menuCallback(CCObject * pSender)
- {
-
- CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
-
- int nIdx = pMenuItem->getZOrder() - 10000;
-
-
- TestScene* pScene = CreateTestScene(nIdx);
- if (pScene)
- {
-
- pScene->runThisTest();
-
- pScene->release();
- }
- }
-
- void TestController::closeCallback(CCObject * pSender)
- {
-
- CCDirector::sharedDirector()->end();
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- }
-
- void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
- {
-
- CCSetIterator it = pTouches->begin();
- CCTouch* touch = (CCTouch*)(*it);
-
- m_tBeginPos = touch->getLocation();
- }
-
- void TestController::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
- {
-
- CCSetIterator it = pTouches->begin();
- CCTouch* touch = (CCTouch*)(*it);
-
- CCPoint touchLocation = touch->getLocation();
- float nMoveY = touchLocation.y - m_tBeginPos.y;
-
- CCPoint curPos = m_pItemMenu->getPosition();
- CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);
-
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- if (nextPos.y < 0.0f)
- {
- m_pItemMenu->setPosition(CCPointZero);
- return;
- }
-
- if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height))
- {
- m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height)));
- return;
- }
-
- m_pItemMenu->setPosition(nextPos);
-
- m_tBeginPos = touchLocation;
- s_tCurPos = nextPos;
- }
总结:示例框架展示了一个菜单,每个菜单项代表了一个示例工程,菜单项的Z值是递增排列的。点击菜单项响应TestController::menuCallback函数,此函数通过减去Z值基数可以取得菜单项的索引,并通过CreateTestScene(nIdx);来创建出相应的示例场景。本示例框架还演示了如何在纵方向上拖动一个菜单。