三: cocos2d-x代码分析
目录结构:
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 启动时初始化的工作
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize Director
CCDirector* pDirector =CCDirector::sharedDirector(); // 场景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //创建视口
pDirector->setOpenGLView(pEGLView); //设置OpenGL视口
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder); //设置分辨率大小
CCSize frameSize =pEGLView->getFrameSize();
vector<string> searchPath; //资源路径
//在不同的平台下加载不同的资源
if (frameSize.height> mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is larger than the height of small resource size, select mediumresource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium resource size, select smallresource.
else
{
searchPath.push_back(smallResource.directory);
pDirector->setContentScaleFactor(MIN( smallResource.size.height/designResolutionSize.height , smallResource.size.width/designResolutionSize.width ) );
}
//设置资源文件路径
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn ondisplay FPS
pDirector->setDisplayStats(true); //开启显示FPS
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 /60); // 设置帧速率 ,最好不要低于30帧
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //调用静态方法创建一个场景,HelloWorld中会负责场景的实现
pDirector->runWithScene(pScene); //导演调用,运行HelloWorld中的场景。
return true;
}
II 暂停动作
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->stopAnimation(); //暂停活动
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暂停背景音乐
}
III 恢复动作
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation(); // 恢复活动
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // 恢复背景音乐
}
2.HelloWorld
CCScene*HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); //创建场景
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //创建图层
// add layer as achild to scene
scene->addChild(layer); //添加图层作为节点
// return the scene
return scene;
}
bool HelloWorld::init()
{
// 1. super initfirst
if (!CCLayer::init() ) //创建图层
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //获取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin(); //获取原点,原点(origin.x , origin.y)在左下角
CCMenuItemImage *pCloseItem =CCMenuItemImage::create( "CloseNormal.png, "CloseSelected.png", this,
menu_selector( HelloWorld::menuCloseCallback ) ); //创建关闭按钮 ,在这里的图片路径如果不是在/Resources 目录下,则图片不能使用。 如果要在Resources目下放置文件夹,则需要在字符串中加入路径名称,如”/raster-32x32/ 32x32.png“
//设置按钮位置,可以看出 Cocos2d-x的坐标原点是左下角
pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y +pCloseItem->getContentSize().height/2));
// visibleSize.width 屏幕的宽度
// pCloseItem->getContentSize().width/2 是图片宽度的1/2
// pCloseItem->getContentSize().height/2 是图片高度的1/2
// pCloseItem->setPosition(ccp(origin.x , origin.y ) ); // pCloseItem的坐标设置是以默认锚点为坐标中心,即锚点在(0,0),此时按钮只能显示1/4
//创建菜单,将关闭按钮加入到菜单项
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero); // 设置菜单的位置
this->addChild(pMenu,1); // 将菜单加入到HelloWorld图层中
//创建 HelloWorld 文本
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);
// position thelabel on the center of the screen
pLabel->setPosition(ccp(origin.x +visibleSize.width/2,
origin.y +visibleSize.height - pLabel->getContentSize().height)); //设置文本(Label)的位置 ,坐标是字符串中心的坐标,并不是最开始的位置
// add the labelas a child to this layer
this->addChild(pLabel,1); // 将文本(Label)加入到HelloWorld图层中
//创建精灵图片
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position thesprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y)); // 设置图片精灵的位置
// add the spriteas a child to this layer
this->addChild(pSprite,0); // 将图片精灵加入到HelloWorld图层中 (第二个参数是Z轴坐标:0在底层,1在上层 。 Z轴坐标系参考笛卡尔右手坐标系(正方向:x轴向右,y轴向上,z轴向外)
//设置为相同的Z轴坐标时,按加载顺序显示,先加载的先显示,后加载的后显示
return true;
}
IIII 点击按钮结束的回调函数
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
转载于:https://blog.51cto.com/murphy/1429772
三: cocos2d-x代码分析相关推荐
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- TCP三次握手代码分析与过程跟踪
https://www.cnblogs.com/luoyang712/p/12099983.html 1.内核TCP三次握手代码分析 首先客户端调用connect主动发起连接 SYSCALL_DEFI ...
- Device Tree(三):代码分析
2019独角兽企业重金招聘Python工程师标准>>> 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请 ...
- cocos2D 程序代码分析 3
在第一讲中已经新建了第一个cocos2d程序,运行效果如下: 在这讲中我们来分析下里面的代码,了解cocos2d的工作原理,看看屏幕上的这个"Hello World"是如何显示出来 ...
- PostgreSQL代码分析,查询优化部分,canonicalize_qual
这里把规范谓词表达式的部分就整理完了.阅读的顺序例如以下: 一.PostgreSQL代码分析,查询优化部分,canonicalize_qual 二.PostgreSQL代码分析,查询优化部分,pull ...
- 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 三 | 等待远程函数执行完毕 | 寄存器获取返回值 )
文章目录 前言 一.等待远程进程 mmap 函数执行完毕 二.从寄存器中获取进程返回值 三.博客资源 前言 前置博客 : [Android 逆向]Android 进程注入工具开发 ( 注入代码分析 | ...
- u-boot分析之两阶段代码分析(三)
目录 u-boot(三)启动文件 1,概述 2,uboot第一阶段代码分析: 汇编 2,uboot第二阶段代码分析 C:_start_armboot C:main_loop u-boot(三)启动文件 ...
- [系统安全] 三十一.恶意代码检测(1)恶意代码攻击溯源及恶意样本分析
您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...
- linux内存映射起始地址,内存初始化代码分析(三):创建系统内存地址映射
内存初始化代码分析(三):创建系统内存地址映射 作者:linuxer 发布于:2016-11-24 12:08 分类:内存管理 一.前言 经过内存初始化代码分析(一)和内存初始化代码分析(二)的过渡, ...
最新文章
- python中字典添加键对_关于python:如何向字典添加键、值对?
- Java基础-异常(Exception)处理
- Docker之docker简介及其优势
- OpenGL模型加载和渲染
- 2018中国C++大会精彩回顾
- ajax默认什么方法,ajax设置默认值ajaxSetup()方法
- 前端工程师和数据科学的快乐
- create显示中文乱码 qt_Ubuntu下Qt串口助手接收中文乱码问题
- 一台电脑连接到另外一台电脑的虚拟机里面的系统
- 去除非ascii字符以及ascii中的控制符
- 在Mac上如何设置自动解压下载的压缩文件?
- 弹性理论法研究桩基受力计算公式_浅谈桩基础沉降计算方法
- 文件误删除怎么恢复?解忧小妙招
- 华为p10 android几,华为p10国行版和海外版有什么区别 配置参数对比评测
- S32K144的FLASH中的SDK函数(FLASH_DRV_EraseSector)不能正常执行
- w7设置双显示器_Win7双显示器设置方法 一台电脑带两个显示器设置方法
- 计算机英语阅读短文,计算机经 典英语短文阅读.pdf
- 全球及中国氢燃料电池汽车行业销售规模与运营前景展望报告2022版
- windows下安装和使用geany
- 什么是企业管理系统,应如何选择?