三: cocos2d-x代码分析
更新:HHH   时间:2023-1-7


目录结构:

    


Box2D
 物理引擎Box2D的相关文件
Chipmunk物理引擎Chipmunk的相关文件
cocos2dxcocos2d-x引擎的核心,存放引擎的大部分源文件
CocosDenshion音频模块相关源文件
Debug.win32 在Windows上的调试输出目录
Doxygen生成doxygen项目文档时需要的配置文件
HelloWorldHelloWorld的源代码
Hellolualua的示例代码
Lua lua脚本支持的源码
JsJs脚本支持的源码
Licences许可文件的目录
Template包括编译Ios和Android平台开发时的配置文件
testjs
cocos2d-x引擎js语言的API示例代码
testscocos2d-x引擎的所有API示例代码
Tools包括“Tolua的配置文件”和“Xcode4的模版生成工具”





1.类AppDelegate   //控制游戏生命周期


    类AppDelegate继承了CCApplication,定义了三个方法:

        virtualbool applicationDidFinishLaunching();         //响应窗口启动完成后的工作


        virtualvoid applicationDidEnterBackground();         //响应窗口进入后台的工作


        virtualvoid applicationWillEnterForeground();          //响应窗口从后台恢复的工作


   I  启动时初始化的工作

  1. bool AppDelegate::applicationDidFinishLaunching()

  2. {

  3.     // initialize Director


  4.     CCDirector* pDirector =CCDirector::sharedDirector();  // 场景管理器


  5.     CCEGLView* pEGLView =CCEGLView::sharedOpenGLView();  //创建视口 


  6.     pDirector->setOpenGLView(pEGLView);          //设置OpenGL视口 


  7.     // Set the design resolution


  8.    pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder);     //设置分辨率大小 


  9.     CCSize frameSize =pEGLView->getFrameSize();    


  10.     vector<string> searchPath;           //资源路径 


  11.  //在不同的平台下加载不同的资源


  12.     if (frameSize.height> mediumResource.size.height)

  13.     {

  14.        searchPath.push_back(largeResource.directory); 


  15.        pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));

  16.     }


  17.     // if the frame'sheight is larger than the height of small resource size, select mediumresource.


  18.     else  if (frameSize.height > smallResource.size.height)

  19.     {

  20.        searchPath.push_back(mediumResource.directory);        


  21.         pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));

  22.     }

  23.     // if the frame's height is smaller than the height of medium resource size, select smallresource.


  24.     else

  25.     {

  26.        searchPath.push_back(smallResource.directory);    

  27.        pDirector->setContentScaleFactor(MIN( smallResource.size.height/designResolutionSize.height , smallResource.size.width/designResolutionSize.width ) );

  28.     }  


  29.   //设置资源文件路径


  30.     CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);   


  31.     // turn ondisplay FPS


  32.     pDirector->setDisplayStats(true);    //开启显示FPS 


  33.     // set FPS. the default value is 1.0/60 if you don't call this


  34.     pDirector->setAnimationInterval(1.0 /60);   // 设置帧速率 ,最好不要低于30帧


  35.     // create a scene. it's an autorelease object


  36.     CCScene *pScene = HelloWorld::scene();      //调用静态方法创建一个场景,HelloWorld中会负责场景的实现  


  37.     pDirector->runWithScene(pScene);      //导演调用,运行HelloWorld中的场景。 


  38.     return  true;

  39. }

II  暂停动作

  1. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too


  2. void AppDelegate::applicationDidEnterBackground()

  3. {

  4.     CCDirector::sharedDirector()->stopAnimation();     //暂停活动


  5.     // if you use SimpleAudioEngine, it must be pause

  6.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();   //暂停背景音乐

  7. }


III  恢复动作

  1. // this function will be called when the app is active again


  2. void AppDelegate::applicationWillEnterForeground()

  3. {

  4.     CCDirector::sharedDirector()->startAnimation();   // 恢复活动


  5.     // if you use SimpleAudioEngine, it must resume here

  6.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();   // 恢复背景音乐

  7. }



2.HelloWorld


  1. CCScene*HelloWorld::scene()

  2. {

  3.     // 'scene' is an autorelease object


  4.     CCScene *scene = CCScene::create();    //创建场景


  5.     // 'layer' is an autorelease object


  6.     HelloWorld *layer = HelloWorld::create();    //创建图层


  7.     // add layer as achild to scene


  8.     scene->addChild(layer);                  //添加图层作为节点 


  9.     // return the scene


  10.     return scene;

  11. }

  12. bool HelloWorld::init()

  13. {

  14.     // 1. super initfirst


  15.     if (!CCLayer::init() )     //创建图层

  16.     {

  17.         return  false;

  18.     }    


  19.     CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize();            //获取大小


  20.     CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin();           //获取原点,原点(origin.x , origin.y)在左下角


  21.     CCMenuItemImage *pCloseItem =CCMenuItemImage::create(  "CloseNormal.png,  "CloseSelected.png",   this,


  22.              menu_selector( HelloWorld::menuCloseCallback ) );      //创建关闭按钮 ,在这里的图片路径如果不是在/Resources 目录下,则图片不能使用。  如果要在Resources目下放置文件夹,则需要在字符串中加入路径名称,如”/raster-32x32/ 32x32.png“


  23. //设置按钮位置,可以看出 Cocos2d-x的坐标原点是左下角


  24. pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,   


  25.                                 origin.y +pCloseItem->getContentSize().height/2));  

  26. //   visibleSize.width                                            屏幕的宽度

  27. //   pCloseItem->getContentSize().width/2     是图片宽度的1/2

  28. //   pCloseItem->getContentSize().height/2    是图片高度的1/2



  29. //      pCloseItem->setPosition(ccp(origin.x ,   origin.y ) );  // pCloseItem的坐标设置是以默认锚点为坐标中心,即锚点在(0,0),此时按钮只能显示1/4


  30.   //创建菜单,将关闭按钮加入到菜单项    


  31.     CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);


  32.     pMenu->setPosition(CCPointZero);    //  设置菜单的位置


  33.     this->addChild(pMenu,1);                  //  将菜单加入到HelloWorld图层中


  34.     //创建 HelloWorld 文本


  35.     CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);   


  36.     // position thelabel on the center of the screen


  37.     pLabel->setPosition(ccp(origin.x +visibleSize.width/2,


  38.                             origin.y +visibleSize.height - pLabel->getContentSize().height));  //设置文本(Label)的位置 ,坐标是字符串中心的坐标,并不是最开始的位置


  39.     // add the labelas a child to this layer


  40.     this->addChild(pLabel,1);   //  将文本(Label)加入到HelloWorld图层中


  41.    //创建精灵图片 


  42.     CCSprite* pSprite = CCSprite::create("HelloWorld.png"); 


  43.     // position thesprite on the center of the screen


  44.    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));    // 设置图片精灵的位置


  45.     // add the spriteas a child to this layer 


  46.     this->addChild(pSprite,0);     //  将图片精灵加入到HelloWorld图层中   (第二个参数是Z轴坐标:0在底层,1在上层  。  Z轴坐标系参考笛卡尔右手坐标系(正方向:x轴向右,y轴向上,z轴向外)

  47. //设置为相同的Z轴坐标时,按加载顺序显示,先加载的先显示,后加载的后显示


  48.     return  true;

  49.   }


IIII  点击按钮结束的回调函数


  1. void HelloWorld::menuCloseCallback(CCObject* pSender)

  2. {

  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

  4. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");

  5. #else

  6.     CCDirector::sharedDirector()->end();

  7. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

  8.     exit(0);

  9. #endif






返回游戏开发教程...