Cocos2dx 3.0 学习笔记:屏幕适配的方法。

来源:互联网 发布:js获取url参数中文乱码 编辑:程序博客网 时间:2024/06/07 21:27

http://www.ttsgs.com/articles/detail/2013-05-12/47804/Cocos2d-x-%E6%B8%B8%E6%88%8F-%E5%9B%BE%E7%89%87

我们都知道现在手机的分辨率越来越杂。480*320  800*480  1280*720 等等分辨率。如果要让游戏运行在所有的屏幕分辨率上,似乎是很困难,美工要为不同的分辨率创建不同分辨率的素材,这样 美工可是要累死的节奏。也是很让人头疼的。 所以coco引进了一个"设计分辨率" 所有素材按照设计分辨率的大小来设计制造。以及在编码时精灵的位置等等 都依据这个"设计分辨率"作为参照。之后如果游戏运行在不同的设备上 coco会自动为目标设备的分辨率进行缩放或者拉伸。这样就做到了屏幕所有的适配。

下面贴一段代码

[cpp] view plaincopy
  1. bool AppDelegate::applicationDidFinishLaunching() {  
  2.     // initialize director  
  3.     auto director = Director::getInstance();  
  4.     auto eglView = EGLView::getInstance();  
  5.     director->setOpenGLView(eglView);  
  6.     eglView->setDesignResolutionSize(480,800,ResolutionPolicy(0)); //设计分辨率为480*800的分辨率 我们的素材也按照480*800作为标准设计。  
  7.     // turn on display FPS  
  8.    director->setDisplayStats(true);  
  9.     // set FPS. the default value is 1.0/60 if you don't call this  
  10.     director->setAnimationInterval(1.0 / 60);  
  11.     // create a scene. it's an autorelease object  
  12.     auto scene = HelloWorld::createScene();  
  13.   
  14.     // run  
  15.     director->runWithScene(scene);  
  16.       
  17.     return true;  
  18. }  

[cpp] view plaincopy
  1. 第三个参数:ResolutionPolicy(0) 因为我这里没有引用这个声明枚举的文件。下面贴一下其声明  
[cpp] view plaincopy
  1. enum class ResolutionPolicy  
  2. {  
  3.     // The entire application is visible in the specified area without trying to preserve the original aspect ratio.  
  4.     // Distortion can occur, and the application may appear stretched or compressed.  
  5.     EXACT_FIT,//缩放拉伸全屏   
  6.     // The entire application fills the specified area, without distortion but possibly with some cropping,  
  7.     // while maintaining the original aspect ratio of the application.  
  8.     NO_BORDER, //不显示操作面板  
  9.     // The entire application is visible in the specified area without distortion while maintaining the original  
  10.     // aspect ratio of the application. Borders can appear on two sides of the application.  
  11.     SHOW_ALL,//显示全部,但是保持宽高比  
  12.     // The application takes the height of the design resolution size and modifies the width of the internal  
  13.     // canvas so that it fits the aspect ratio of the device  
  14.     // no distortion will occur however you must make sure your application works on different  
  15.     // aspect ratios  
  16.     FIXED_HEIGHT,//高度不变  
  17.     // The application takes the width of the design resolution size and modifies the height of the internal  
  18.     // canvas so that it fits the aspect ratio of the device  
  19.     // no distortion will occur however you must make sure your application works on different  
  20.     // aspect ratios  
  21.     FIXED_WIDTH,//宽度不变  
  22.   
  23.     UNKNOWN,  
  24. };  

这里面我试过就 EXACT_FIT 和NO_BORDER效果是一样的 SHOW_ALL 是保持素材的宽高比 但是 上下方或者左右方可能出现黑条 最后面两个可以忽略,实用性基本没有。

然后在入口函数设置宽高分辨率,因为我这是在win下 在安卓下入口不一样 并且宽度和高度肯定是要调用某些API获取(还没弄android抱歉)

[cpp] view plaincopy
  1. int APIENTRY _tWinMain(HINSTANCE hInstance,  
  2.                        HINSTANCE hPrevInstance,  
  3.                        LPTSTR    lpCmdLine,  
  4.                        int       nCmdShow)  
  5. {  
  6.     UNREFERENCED_PARAMETER(hPrevInstance);  
  7.     UNREFERENCED_PARAMETER(lpCmdLine);  
  8.   
  9.     // create the application instance  
  10.     AppDelegate app;  
  11.     EGLView eglView;  
  12.     eglView.init("Test",320,480); //设置其分辨率为320*480  
  13.     return Application::getInstance()->run();  

运行可以看到,适配成功 不管多少分辨率coco会为我们拉伸或者缩放。

下面图素材是480*800,分别使用320*480 和240*320测试。






http://blog.csdn.net/yilusuiyun/article/details/19755711
0 0