cocos2dx基础篇(3) 常用重要类

  • 入口类 main.cpp
  • 主要控制类 AppDelegate.cpp
  • 对象类 CCObject
  • 节点类 CCNode

入口类main.cpp

    这是应用程序的入口类,用于创建cocos2dx的AppDelegate实例、窗口大小、以及运行程序。

    主要代码如下:

	// 创建一个主控制类AppDelegate
    AppDelegate app;  
	// 使用OpenGL进行图形渲染
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
	// 窗口名
    eglView->setViewName("CocosStudy");
	// 窗口大小
    eglView->setFrameSize(480, 320);
	// 运行
    return CCApplication::sharedApplication()->run();

主要控制类AppDelegate.cpp

    游戏的入口,用于游戏的初始化,并创建第一个游戏界面。

    里面有3个方法:

	// 初始化
    virtual bool applicationDidFinishLaunching();

	// 切换到后台
    virtual void applicationDidEnterBackground();

	// 切换到前台
    virtual void applicationWillEnterForeground();

    源码:

// 初始化
bool AppDelegate::applicationDidFinishLaunching() { 

	// 创建导演
	CCDirector* pDirector = CCDirector::sharedDirector();
	// 使用OpenGLView
	pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
	// 设置游戏的设计分辨率
	CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll);

	// 关闭帧数显示
	pDirector->setDisplayStats(false);

	// 刷新频率,每秒60帧
	pDirector->setAnimationInterval(1.0 / 60);

	// 创建一个场景HelloWorld,游戏程序的第一个界面
	CCScene *pScene = HelloWorld::scene();

	// 运行场景
	pDirector->runWithScene(pScene);

	return true;
}

// 切换到后台
void AppDelegate::applicationDidEnterBackground() {

	// 暂停游戏
	CCDirector::sharedDirector()->stopAnimation();

	// 暂停音乐,需要时打开
	//SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// 切换到前台
void AppDelegate::applicationWillEnterForeground() {

	// 游戏恢复
	CCDirector::sharedDirector()->startAnimation();

	// 音乐恢复,需要时打开
	//SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

  在main.cpp中,我们设置了分辨率“eglView->setFrameSize(480, 320)”(1),在这里我们又设置了分辨率“CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480,320,kResolutionShowAll)”(2),但是它们的意义不同。(1)是根据我们的期望屏幕大小设置的,(2)是设计时的游戏分辨率,我们最初的设计是在电脑上完成的,这个大小是在电脑上显示的大小,但是游戏移植到手机后,手机型号不同,所以后面的参数kResolutionShowAll表示按照原比例(480x320)进行缩放来适配手机屏幕。

    如果图片的大小为1x1,setFrameSize(1, 1),那么setDesignResolutionSize(1,1)后,即使没有设置kResolutionShowAll,图片也能铺满整个屏幕。但是,如果setFrameSize(2, 2),如果setDesignResolutionSize(2,2)后,如果没有设置kResolutionShowAll,那么图片只会铺满屏幕的1/4,只有设置kResolutionShowAll,屏幕才会缩放到整个屏幕。

猜你喜欢

转载自blog.csdn.net/qq_40338728/article/details/81082296