zoukankan      html  css  js  c++  java
  • 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();  //回复全部事件
    }



  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10632553.html
Copyright © 2011-2022 走看看