目標: 建立一個測試工程,測試工程以列表的方式展現,沒一個列表項對應一個場景css
1. 建立cocos2d-x工程html
如今採用腳本的方式來建立,好處是一次能夠建立N個項目的工程。node
首先要安裝Python , 官網既可 下載,我下載的是 V2.7.5 X64,下載完成後安裝到本地。python
配置環境變量: ios
打開 計算機-> 屬性-> 高級系統設置 -> 環境變量 ,在系統變量裏找到 Path 這一項,在後面添加 一句:app
D:\DevTools\Python;iphone
其中後面那個是你安裝的Python的目錄,而後打開 命令行,按照下圖,測試
首先定位到 cocos2d-x目錄下的 tools\project-creator, 而後輸入 對應的腳本 ui
python create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language cppthis
MyGame 是你的遊戲的名稱 com.MyCompany.Awesome 是安卓,ios裏用到的包名,按需修改,其它的照填就能夠了。
完成以後打開 cocos2d-x 目錄下的 project 文件夾,看到以下所示,進入 project.win32 就是咱們的開發環境了,
打開 *.sln ,熟悉的界面來了,小夥伴們趕忙動手把。
最近剛纔發現一個問題,由於這個項目都是拷貝的模板來的,因此應用的惟一標識都是一個。這對於發佈應用沒有什麼影響,由於微軟會對應用進行從新簽名打包。
可是在調試的時候,後一個應用會自動覆蓋前一個應用,若是要破,請自行建立一個新應用,而後替換惟一標識!
2. 實現原理:
AppDelegate.cpp ---> 添加列子控制層TestController.cpp ----> 沒添加一個場景測試項目,則添加一個對應的菜單 ---> 點擊菜單啓動場景TestScene。
每個場景測試項目都繼承TestScene,而後實現方法:runThisTest
3. AppDelegate.cpp 說明
1: #include "AppDelegate.h"2: #include "controller.h"3:
4: USING_NS_CC;
5:
6: AppDelegate::AppDelegate() {
7:
8: }
9:
10: AppDelegate::~AppDelegate()
11: {
12: }
13:
14: bool AppDelegate::applicationDidFinishLaunching() {15: // initialize director16: CCDirector* pDirector = CCDirector::sharedDirector();
17:
18: CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
19:
20: pDirector->setOpenGLView(pEGLView);
21:
22: // turn on display FPS23: pDirector->setDisplayStats(true);24:
25: // set FPS. the default value is 1.0/60 if you don't call this26: pDirector->setAnimationInterval(1.0 / 60);
27:
28: CCScene * pScene = CCScene::create();
29:
30: CCLayer * pLayer = new TestController();31: pLayer->autorelease(); //這裏爲何play要放到自動釋放,而pScene 不須要,由於pScene用的靜態工廠方法建立的32:
33: pScene->addChild(pLayer);
34: pDirector->runWithScene(pScene);
35:
36:
37: return true;38: }
39:
40: // This function will be called when the app is inactive. When comes a phone call,it's be invoked too41: void AppDelegate::applicationDidEnterBackground() {42: CCDirector::sharedDirector()->stopAnimation();
43:
44: // if you use SimpleAudioEngine, it must be pause45: // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();46: }
47:
48: // this function will be called when the app is active again49: void AppDelegate::applicationWillEnterForeground() {50: CCDirector::sharedDirector()->startAnimation();
51:
52: // if you use SimpleAudioEngine, it must resume here53: // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();54: }
4. 控制器
1: #ifndef _CONTROLLER_H_
2: #define _CONTROLLER_H_3:
4: #include "cocos2d.h"5:
6: USING_NS_CC;
7:
8: class TestController : public CCLayer9: {
10: public:11: TestController();
12: ~TestController();
13:
14:
15: void menuCallback(CCObject * pSender);16: void closeCallback(CCObject * pSender);17:
18: virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);19: virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);20:
21: private:22: CCPoint m_tBeginPos;
23: CCMenu* m_pItemMenu;
24: };
25:
26: #endif1: #include "controller.h"2: #include "tests.h"3: #include "testResource.h"4: #include "testBasic.h"5:
6: #define LINE_SPACE 407:
8: static CCPoint s_tCurPos = CCPointZero;9:
10: /************************************************************************/11: /* 根據傳入的加載項目的ID來加載對於的場景 */12: /************************************************************************/13: static TestScene* CreateTestScene(int nIdx)14: {
15: CCDirector::sharedDirector()->purgeCachedData();
16:
17: TestScene* pScene = NULL;
18:
19: switch (nIdx)20: {
21: case HelloWorld: //在tests.h中枚舉定義22: pScene = new ZwHelloWorldScene(); break;23: default:24: break;25: }
26:
27: return pScene;28: }
29:
30: TestController::TestController()
31: : m_tBeginPos(CCPointZero)
32: {//類後面添加 : m_tBeginPos(CCPointZero) 做用是初始化m_tBeginPos變量33:
34: // add close menu35: CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseSelected.png", "CloseSelected.png", this, menu_selector(TestController::closeCallback) );36: CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);
37: pMenu->setPosition( CCPointZero );
38: pCloseItem->setPosition(ccp( VisibleRect::right().x - 30, VisibleRect::top().y - 30));
39:
40: // 每個增長的測試項目對應一個菜單項,測試項目的名稱和數量在tests.h中定義41: m_pItemMenu = CCMenu::create();
42: for (int i = 0; i < TESTS_COUNT; ++i)43: {
44:
45: CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24);46:
47: CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));48:
49: m_pItemMenu->addChild(pMenuItem, i + 10000);
50: pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
51: }
52:
53: m_pItemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (TESTS_COUNT + 1) * (LINE_SPACE)));
54: m_pItemMenu->setPosition(s_tCurPos);
55: addChild(m_pItemMenu);
56:
57: setTouchEnabled(true);58:
59: addChild(pMenu, 1);
60:
61: //#define CC_PLATFORM_WINRT_SAVE_SHADERS62: /*63: #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) && defined(CC_PLATFORM_WINRT_SAVE_SHADERS)64: ShaderTestDemo::precompileShaders();65: CCPrecompiledShaders::sharedPrecompiledShaders()->savePrecompiledShaders();66: #endif67: */68: }
69:
70: TestController::~TestController()
71: {
72: }
73:
74: /************************************************************************/75: /* 點擊測試項目菜單項之後,加載對於的場景 */76: /************************************************************************/77: void TestController::menuCallback(CCObject * pSender)78: {
79: // get the userdata, it's the index of the menu item clicked80: CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
81: int nIdx = pMenuItem->getZOrder() - 10000;82:
83: // create the test scene and run it84: TestScene* pScene = CreateTestScene(nIdx);
85: if (pScene)86: {
87: pScene->runThisTest();
88: pScene->release();
89: }
90: }
91:
92: void TestController::closeCallback(CCObject * pSender)93: {
94: #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)95: CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");96:
97: #else98: CCDirector::sharedDirector()->end();
99: #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)100: exit(0);
101: #endif102: #endif103: }
104:
105: void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)106: {
107: CCSetIterator it = pTouches->begin();
108: CCTouch* touch = (CCTouch*)(*it);
109:
110: m_tBeginPos = touch->getLocation();
111: }
112:
113: void TestController::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)114: {
115: CCSetIterator it = pTouches->begin();
116: CCTouch* touch = (CCTouch*)(*it);
117:
118: CCPoint touchLocation = touch->getLocation();
119: float nMoveY = touchLocation.y - m_tBeginPos.y;120:
121: CCPoint curPos = m_pItemMenu->getPosition();
122: CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);
123:
124: if (nextPos.y < 0.0f)125: {
126: m_pItemMenu->setPosition(CCPointZero);
127: return;128: }
129:
130: if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))131: {
132: m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
133: return;134: }
135:
136: m_pItemMenu->setPosition(nextPos);
137: m_tBeginPos = touchLocation;
138: s_tCurPos = nextPos;
139: }
5. 場景容器
1: #ifndef _TEST_BASIC_H_
2: #define _TEST_BASIC_H_3:
4: #include "cocos2d.h"5: #include "VisibleRect.h"6:
7: USING_NS_CC;
8: using namespace std;9:
10: class TestScene : public CCScene11: {
12: public:13: TestScene(bool bPortrait = false);14: virtual void onEnter();15:
16: virtual void runThisTest() = 0;17:
18: // The CallBack for back to the main menu scene19: virtual void MainMenuCallback(CCObject* pSender);20: };
21:
22: typedef CCLayer* (*NEWTESTFUNC)();
23: #define TESTLAYER_CREATE_FUNC(className) \24: static CCLayer* create##className() \25: { return new className(); }26:
27: #define CF(className) create##className28:
29: #endif1: #include "testBasic.h"2: #include "controller.h"3:
4: TestScene::TestScene(bool bPortrait)5: {
6:
7: CCScene::init();
8: }
9:
10: void TestScene::onEnter()11: {
12: CCScene::onEnter();
13:
14: //add the menu item for back to main menu15: //#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)16: // CCLabelBMFont* label = CCLabelBMFont::create("MainMenu", "fonts/arial16.fnt");17: //#else18: CCLabelTTF* label = CCLabelTTF::create("MainMenu", "Arial", 20);19: //#endif20: CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestScene::MainMenuCallback));21:
22: CCMenu* pMenu =CCMenu::create(pMenuItem, NULL);
23:
24: pMenu->setPosition( CCPointZero );
25: pMenuItem->setPosition( ccp( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) );
26:
27: addChild(pMenu, 1);
28: }
29:
30: void TestScene::MainMenuCallback(CCObject* pSender)31: {
32: CCScene* pScene = CCScene::create();
33: CCLayer* pLayer = new TestController();34: pLayer->autorelease();
35:
36: pScene->addChild(pLayer);
37: CCDirector::sharedDirector()->replaceScene(pScene);
38: }
6. 加載項目定義
1: #ifndef _TESTS_H_
2: #define _TESTS_H_3:
4: #include "HelloWorld/HelloWorldScene.h"5:
6: enum7: {
8: HelloWorld = 0,
9: // last one10: TESTS_COUNT,
11: };
12:
13: const std::string g_aTestNames[TESTS_COUNT] = {14: "HelloWorld"15: };
16:
17: #endif7. 第一個hellowold
1: #ifndef __HELLOWORLD_SCENE_H__
2: #define __HELLOWORLD_SCENE_H__3:
4: #include "cocos2d.h"5: #include "../testBasic.h"6:
7: class HelloWorld : public cocos2d::CCLayer8: {
9: public:10: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone11: virtual bool init();12:
13: // there's no 'id' in cpp, so we recommend returning the class instance pointer14: static cocos2d::CCScene* scene();15:
16: // a selector callback17: void menuCloseCallback(CCObject* pSender);18:
19: // implement the "static node()" method manually20: CREATE_FUNC(HelloWorld);
21: };
22:
23: class ZwHelloWorldScene : public TestScene24: {
25: public:26: virtual void runThisTest();27: };
28:
29: #endif // __HELLOWORLD_SCENE_H__
1: #include "HelloWorld/HelloWorldScene.h"
2:
3: USING_NS_CC;
4:
5: CCScene* HelloWorld::scene()
6: {
7: // 'scene' is an autorelease object
8: CCScene *scene = CCScene::create();
9:
10: // 'layer' is an autorelease object
11: HelloWorld *layer = HelloWorld::create();
12:
13: // add layer as a child to scene
14: scene->addChild(layer);
15:
16: // return the scene
17: return scene;
18: }
19:
20: // on "init" you need to initialize your instance
21: bool HelloWorld::init()
22: {
23: //////////////////////////////
24: // 1. super init first
25: if ( !CCLayer::init() )
26: {
27: return false;
28: }
29:
30: CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
31: CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
32:
33: /////////////////////////////
34: // 2. add a menu item with "X" image, which is clicked to quit the program
35: // you may modify it.
36:
37: // add a "close" icon to exit the progress. it's an autorelease object
38: CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
39: "CloseNormal.png",
40: "CloseSelected.png",
41: this,
42: menu_selector(HelloWorld::menuCloseCallback));
43:
44: pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
45: origin.y + pCloseItem->getContentSize().height/2));
46:
47: // create menu, it's an autorelease object
48: CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
49: pMenu->setPosition(CCPointZero);
50: this->addChild(pMenu, 1);
51:
52: /////////////////////////////
53: // 3. add your codes below...
54:
55: // add a label shows "Hello World"
56: // create and initialize a label
57:
58: CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
59:
60: // position the label on the center of the screen
61: pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
62: origin.y + visibleSize.height - pLabel->getContentSize().height));
63:
64: // add the label as a child to this layer
65: this->addChild(pLabel, 1);
66:
67: // add "HelloWorld" splash screen"
68: CCSprite* pSprite = CCSprite::create("HelloWorld.png");
69:
70: // position the sprite on the center of the screen
71: pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
72:
73: // add the sprite as a child to this layer
74: this->addChild(pSprite, 0);
75:
76: return true;
77: }
78:
79:
80: void HelloWorld::menuCloseCallback(CCObject* pSender)
81: {
82: #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
83: CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
84: #else
85: CCDirector::sharedDirector()->end();
86: #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
87: exit(0);
88: #endif
89: #endif
90: }
91:
92: void ZwHelloWorldScene ::runThisTest()
93: {
94: HelloWorld *layer = HelloWorld::create();
95:
96: // add layer as a child to scene
97: this->addChild(layer);
98:
99: CCDirector::sharedDirector()->replaceScene(this);
100: }