Cocos2d-x学习笔记(17)(TestCpp源代码分析-1)

TestCpp源代码基于Cocos2d-x2.1.3版本号。部分资源来自红孩儿的游戏编程之路CSDN博客地址http://blog.csdn.net/honghaier/article/details/8130947

在VS2010中展开TestCppproject,以下包括46个演示样例文件夹。除此之外。还包括:(1)AppDelegate.h/cpp:程序控制类AppDelegate。(2)controller.h/cpp:演示样例场景管理类TestController,用于显示全部演示样例的菜单。

(3)testBasic.h/cpp:演示样例场景基类TestScene。用于返回到主界面场景。

(4)testResource.h:文件资源名称字符串定义头文件(5)tests.h:演示样例总头文件。(6)main.h/cpp:主函数及头文件。以下具体介绍下每一个文件。

(1)AppDelegate.h/cpp:这两个文件是程序控制类AppDelegate。具体代码例如以下:

//AppDelegate.h
#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_
#include "cocos2d.h"
class  AppDelegate : private cocos2d::CCApplication  //AppDelegate类私有继承于CCApplication
{
public:
    AppDelegate();  //构造函数
    virtual ~AppDelegate();  //析构函数
    virtual bool applicationDidFinishLaunching();  //启动应用程序后将调用这种方法。默认的实现中已经包括了游戏启动后的必要准备
    virtual void applicationDidEnterBackground();  //应用程序进入后台时。会调用这种方法
    virtual void applicationWillEnterForeground();  //该方法与上个方法成对出现。应用程序返回前台时被调用。
};
#endif // _APP_DELEGATE_H_
这个 头文件与学习笔记1介绍的一样。

以下分析下cpp文件。

#include "AppDelegate.h"
#include "cocos2d.h"
#include "controller.h"  //显示菜单的头文件
#include "SimpleAudioEngine.h"  //背景音乐播放头文件

USING_NS_CC;
using namespace CocosDenshion; //声明命名空间。以使用SimpleAudioEngine.h

AppDelegate::AppDelegate()  //构造函数
{
}

AppDelegate::~AppDelegate()  //析构函数
{
//    SimpleAudioEngine::end();
}
bool AppDelegate::applicationDidFinishLaunching()  //启动程序调用函数
{
    // initialize director:初始化游戏引擎控制器CCDirector。以便启动游戏引擎
    CCDirector *pDirector = CCDirector::sharedDirector();  
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());  //设置场景显示

    CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();  //获取屏幕大小

    CCSize designSize = CCSizeMake(480, 320);  //设计屏幕大小
    CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils();//临时为搞明确该类
    
    if (screenSize.height > 320)  //
    {
        CCSize resourceSize = CCSizeMake(960, 640);
        std::vector<std::string> searchPaths;
        searchPaths.push_back("hd");
        pFileUtils->setSearchPaths(searchPaths);
        pDirector->setContentScaleFactor(resourceSize.height/designSize.height);//设置屏幕匹配场景
    }
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
    // turn on display 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);  //设置画图间隔。即屏幕刷新频率
    CCScene * pScene = CCScene::create();  //创建场景
    CCLayer * pLayer = new TestController();  //创建一个TestController层,用于显示菜单
    pLayer->autorelease();  //使用回收池释放层的内存
    pScene->addChild(pLayer);  //将层增加到场景中
    pDirector->runWithScene(pScene);  //执行该场景
    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()  //程序进入后台时调用该方法
{
    CCDirector::sharedDirector()->stopAnimation();  //暂停动作
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  //暂停背景音乐
    SimpleAudioEngine::sharedEngine()->pauseAllEffects();  //暂停全部事件
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()  //程序返回前台使用该方法
{
    CCDirector::sharedDirector()->startAnimation();  //重新启动动作
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  //回复背景音乐
    SimpleAudioEngine::sharedEngine()->resumeAllEffects();  //回复全部事件
}



转载于:https://www.cnblogs.com/mqxnongmin/p/10632553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值