cocos2dx[3.8] ——入口类AppDelegate.cpp
2015-10-28 17:46
543 查看
AppDelegate.cpp,这是游戏程序的入口,主要用于游戏程序的逻辑初始化,并创建运行程序的入口界面(即第一个游戏界面场景)。
AppDelegate类似于android的Application的作用,提供一些应用程序级别的状态的回调,整个游戏应用程序由这个文件方法进行控制。
AppDelegate.cpp里面有三个方法:
initGLContextAttrs();//设置
OpenGL环境
applicationDidFinishLaunching(); //逻辑初始化
applicationDidEnterBackground(); //切换到后台
applicationWillEnterForeground(); //切换到前台
AppDelegate.cpp 代码详解:
AppDelegate类似于android的Application的作用,提供一些应用程序级别的状态的回调,整个游戏应用程序由这个文件方法进行控制。
AppDelegate.cpp里面有三个方法:
initGLContextAttrs();//设置
OpenGL环境
applicationDidFinishLaunching(); //逻辑初始化
applicationDidEnterBackground(); //切换到后台
applicationWillEnterForeground(); //切换到前台
AppDelegate.cpp 代码详解:
<span style="font-size:18px;">#include "AppDelegate.h" #include "HelloWorldScene.h" //命名空间宏,using name space cocos2d; USING_NS_CC; //设置默认分辨率及三种尺寸分辨率,用于适配 static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } //设置 OpenGL环境,这个设置对所有平台都有效 void AppDelegate::initGLContextAttrs() { //设置OpenGL context 属性,目前只能设置6个属性 //red,green,blue,alpha,depth,stencil GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; GLView::setGLContextAttrs(glContextAttrs); } // If you want to use packages manager to install more packages, // don't modify or remove this function static int register_all_packages() { return 0; //flag for packages manager } //当应用程序启动时执行,游戏程序启动入口 //在这里我们启动了第一个scene bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) glview = GLViewImpl::createWithRect("Test", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); #else glview = GLViewImpl::create("Test"); #endif director->setOpenGLView(glview); } // 在屏幕上显示FPS数 // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); //分辨率适配 // Set the design resolution glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); Size frameSize = glview->getFrameSize(); // if the frame's height is larger than the height of medium size. if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); } register_all_packages(); //创建一个HelloWorld的scene,程序运行的第一个场景 // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene); return true; } // 切换到后台 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { //暂停游戏 Director::getInstance()->stopAnimation(); //暂停音乐 // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // 切换到前台 // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { //继续游戏 Director::getInstance()->startAnimation(); //继续音乐 // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } </span>
相关文章推荐
- Android Manifest 用法
- 我是运营,我没有假期
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- 跨越fso、wsh、 application写文件
- VB实现的《QQ美女找茬游戏》作弊器实例
- IIS 错误 Server Application Error 详细解决方法
- asp 内置对象 Application 详解
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法
- 解决IIS的Server Application Error的2种方法
- C#实现洗牌游戏实例
- C#实现的算24点游戏算法实例分析
- C#实现简单的井字游戏实例
- C++编写简单的打靶游戏
- Cocos2d-x中背景音乐和音效使用实例
- C++实现基于控制台界面的吃豆子游戏
- Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验