目录结构:

    

Box2D  物理引擎Box2D的相关文件
Chipmunk 物理引擎Chipmunk的相关文件
cocos2dx cocos2d-x引擎的核心,存放引擎的大部分源文件
CocosDenshion 音频模块相关源文件
Debug.win32  在Windows上的调试输出目录
Doxygen 生成doxygen项目文档时需要的配置文件
HelloWorld HelloWorld的源代码
Hellolua lua的示例代码
Lua  lua脚本支持的源码
Js Js脚本支持的源码
Licences 许可文件的目录
Template 包括编译Ios和Android平台开发时的配置文件
testjs cocos2d-x引擎js语言的API示例代码
tests cocos2d-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



转载于:https://blog.51cto.com/murphy/1429772

三: cocos2d-x代码分析相关推荐

  1. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  2. TCP三次握手代码分析与过程跟踪

    https://www.cnblogs.com/luoyang712/p/12099983.html 1.内核TCP三次握手代码分析 首先客户端调用connect主动发起连接 SYSCALL_DEFI ...

  3. Device Tree(三):代码分析

    2019独角兽企业重金招聘Python工程师标准>>> 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请 ...

  4. cocos2D 程序代码分析 3

    在第一讲中已经新建了第一个cocos2d程序,运行效果如下: 在这讲中我们来分析下里面的代码,了解cocos2d的工作原理,看看屏幕上的这个"Hello World"是如何显示出来 ...

  5. PostgreSQL代码分析,查询优化部分,canonicalize_qual

    这里把规范谓词表达式的部分就整理完了.阅读的顺序例如以下: 一.PostgreSQL代码分析,查询优化部分,canonicalize_qual 二.PostgreSQL代码分析,查询优化部分,pull ...

  6. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 三 | 等待远程函数执行完毕 | 寄存器获取返回值 )

    文章目录 前言 一.等待远程进程 mmap 函数执行完毕 二.从寄存器中获取进程返回值 三.博客资源 前言 前置博客 : [Android 逆向]Android 进程注入工具开发 ( 注入代码分析 | ...

  7. u-boot分析之两阶段代码分析(三)

    目录 u-boot(三)启动文件 1,概述 2,uboot第一阶段代码分析: 汇编 2,uboot第二阶段代码分析 C:_start_armboot C:main_loop u-boot(三)启动文件 ...

  8. [系统安全] 三十一.恶意代码检测(1)恶意代码攻击溯源及恶意样本分析

    您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...

  9. linux内存映射起始地址,内存初始化代码分析(三):创建系统内存地址映射

    内存初始化代码分析(三):创建系统内存地址映射 作者:linuxer 发布于:2016-11-24 12:08 分类:内存管理 一.前言 经过内存初始化代码分析(一)和内存初始化代码分析(二)的过渡, ...

最新文章

  1. python中字典添加键对_关于python:如何向字典添加键、值对?
  2. Java基础-异常(Exception)处理
  3. Docker之docker简介及其优势
  4. OpenGL模型加载和渲染
  5. 2018中国C++大会精彩回顾
  6. ajax默认什么方法,ajax设置默认值ajaxSetup()方法
  7. 前端工程师和数据科学的快乐
  8. create显示中文乱码 qt_Ubuntu下Qt串口助手接收中文乱码问题
  9. 一台电脑连接到另外一台电脑的虚拟机里面的系统
  10. 去除非ascii字符以及ascii中的控制符
  11. 在Mac上如何设置自动解压下载的压缩文件?
  12. 弹性理论法研究桩基受力计算公式_浅谈桩基础沉降计算方法
  13. 文件误删除怎么恢复?解忧小妙招
  14. 华为p10 android几,华为p10国行版和海外版有什么区别 配置参数对比评测
  15. S32K144的FLASH中的SDK函数(FLASH_DRV_EraseSector)不能正常执行
  16. w7设置双显示器_Win7双显示器设置方法 一台电脑带两个显示器设置方法
  17. 计算机英语阅读短文,计算机经 典英语短文阅读.pdf
  18. 全球及中国氢燃料电池汽车行业销售规模与运营前景展望报告2022版
  19. windows下安装和使用geany
  20. 什么是企业管理系统,应如何选择?

热门文章

  1. 【Enterprise Manager 12c】如何在EM 12c中配置Exadata Infiniband告警邮件
  2. Linux下 su命令与su - 命令的区别
  3. 给自己的Sublime Text换上Soda Theme主题后清爽了好多
  4. 简述RHEL7新特性(一)
  5. hdoj1002解题报告
  6. Socket桥(转载)
  7. Android 定制RadioButton样式
  8. RMAN快速入门指南
  9. js对象数组(JSON) 根据某个共同字段 分组
  10. Linus Torvalds:Linux背后的智者