Home >Database >Mysql Tutorial >Cocos2d-x学习笔记(17)(TestCpp源码分析-1)

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

WBOY
WBOYOriginal
2016-06-07 15:01:061011browse

TestCpp源码基于Cocos2d-x2.1.3版本,部分资源来自红孩儿的游戏编程之路CSDN博客地址http://blog.csdn.net/honghaier/article/details/8130947 在VS2010中展开TestCpp工程,下面包含46个示例目录,除此之外,还包含:(1) AppDelegate.h/cpp:程序控制类App

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

在VS2010中展开TestCpp工程,下面包含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<: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();  //回复所有事件
}</:string>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn