1
游戏逻辑架构
具体介绍
A |
B sceneàaddChild(layer); layeràaddChild(sprite); |
2
项目创建命令:
A E:\Installed\cocos2d-x-2.2.3\tools\project-creator> |
B python create_project.py -project MyCocos2dx -package com.toto.mycocos01 -language cpp |
C -project MyCocos2dx工程名 -package com.toto.mycocos01 -language cpp |
D |
3
简单介绍
1 创建了一个cocos2dx项目之后,打开项目之后,会有例如以下项目结构 展开libcocos2d,找到cocos2d.cpp,双击打开此cpp文件,内容例如以下: #include NS_CC_BEGIN const { return } NS_CC_END 截图例如以下: 分析: A B |
2 |
程序入口是:main.cpp |
#include #include #include USING_NS_CC; int HINSTANCE LPTSTR int { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // create the application instance AppDelegate CCEGLView* eglView->setViewName("MyCocos2dx"); eglView->setFrameSize(480, return } |
进入run函数, |
int { PVRFrameEnableControlWindow(false); // Main message loop: MSG LARGE_INTEGER LARGE_INTEGER LARGE_INTEGER QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nLast); // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) { return 0; } CCEGLView* pMainWnd->centerWindow(); ShowWindow(pMainWnd->getHWnd(), while (1) { if (! { // Get current time tick. QueryPerformanceCounter(&nNow); // If it's the time to draw next frame, draw it, else sleep a while. if (nNow.QuadPart { nLast.QuadPart CCDirector::sharedDirector()->mainLoop(); } else { Sleep(0); } continue; } if (WM_QUIT { // Quit message loop. break; } // Deal with windows message. if (! { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) } |
程序的入口:applicationDidFinishLaunching() |
AppDelegate.cpp bool // initialize director CCDirector* CCEGLView* pDirector->setOpenGLView(pEGLView); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 // create a scene. it's an autorelease object CCScene *pScene // run pDirector->runWithScene(pScene); return } 截图: |
HelloWorldScene.h |
#ifndef #define #include class { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual // there's no 'id' in cpp, so we recommend returning the class instance pointer static // a selector callback void // implement the "static node()" method manually CREATE_FUNC(HelloWorld); }; #endif |
HelloWorldScene.cpp |
#include USING_NS_CC; CCScene* { // 'scene' is an autorelease object CCScene *scene // 'layer' is an autorelease object HelloWorld *layer // add layer as a child to scene scene->addChild(layer); //return the scene return } // on "init" you need to initialize your instance bool { ////////////////////////////// // 1. super init first if ( !CCLayer::init() { return } CCSize CCPoint ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x origin.y // create menu, it's an autorelease object CCMenu* pMenu->setPosition(CCPointZero); this->addChild(pMenu, ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* // position the label on the center of the screen pLabel->setPosition(ccp(origin.x origin.y // add the label as a child to this layer this->addChild(pLabel, // add "HelloWorld" splash screen" CCSprite* // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 // add the sprite as a child to this layer this->addChild(pSprite, return } void { #if (CC_TARGET_PLATFORM CCMessageBox("You #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM exit(0); #endif #endif } |
总结: 1、对于cocos真正的初始化是在init()方法中 2、CCScene中的 3、CCPointZero |
4
(CCApplicationProtocol,CCApplication,AppDelegate)三个类的类关系介绍:
抽出代码详细实现:
长处:屏蔽了平台的差异性,实现跨平台
1 #ifndef #define NS_CC_BEGIN enum { kTargetWindows, kTargetLinux, kTargetMacOS, kTargetAndroid, kTargetIphone, kTargetIpad, kTargetBlackBerry, kTargetNaCl, kTargetEmscripten, kTargetTizen, kTargetWinRT, kTargetWP8 }; /** * @addtogroup platform * @{ * @js NA * @lua NA */ class { public: virtual ~CCApplicationProtocol() /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual /** @brief The function be called when the application enter background @param the pointer of the application */ virtual /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual /** @brief Callback by CCDirector for limit FPS. @interval The time, expressed in seconds, between current frame and next. */ virtual /** @brief Get current language config @return Current language config */ virtual /** @brief Get target platform */ virtual }; // end of platform group /// @} NS_CC_END #endif |
2 |
3 |