Cocos2dx进阶学习之屏幕适配

前端之家收集整理的这篇文章主要介绍了Cocos2dx进阶学习之屏幕适配前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://cocos2d.9tech.cn/news/2014/0307/39995.html


@H_502_6@ 背景

@H_502_6@ 在学习cocos2dx时,我们在main函数中发现一句代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "main.h" @H_403_105@
#include "AppDelegate.h" @H_403_105@
#include "CCEGLView.h" @H_403_105@
@H_403_105@
USING_NS_CC; @H_403_105@
@H_403_105@
int APIENTRY _tWinMain(HINSTANCE hInstance,@H_403_105@
@H_403_105@ HINSTANCE hPrevInstance,@H_403_105@
LPTSTR lpCmdLine,@H_403_105@
int nCmdShow) @H_403_105@
{ @H_403_105@
UNREFERENCED_PARAMETER(hPrevInstance); @H_403_105@
UNREFERENCED_PARAMETER(lpCmdLine); @H_403_105@
@H_403_105@
@H_403_105@ // create the application instance @H_403_105@
AppDelegate app; @H_403_105@
CCEGLView* eglView = CCEGLView::sharedOpenGLView(); @H_403_105@
eglView->setViewName(@H_403_105@ "CrazyMario"@H_403_105@ ); @H_403_105@
eglView->setFrameSize(480,320); @H_403_105@
@H_403_105@ return@H_403_105@ CCApplication::sharedApplication()->run(); @H_403_105@
}@H_403_105@
@H_502_6@ 那就是eglView->setFrameSize(480,320),这句代码设置了窗口的大小,一般说来手机游戏需要全屏显示,所以对于不同分辨率的手机,setFrameSize要求不一样的。这样是不是很崩溃?因为我们代码里很多地方可能也要改,图片大小可能也要改,那怎么办呢?

@H_502_6@ 其实cocos2dx已经为我们做好了这一切

@H_502_6@ 结局方案

@H_502_6@ 这个方案就是调用eglView->setDesignResolutionSize(480,320,kResolutionShowAll);来告诉cocos2dx,我的程序是按照480,320来设计的,那么setFrameSize如果不是480,320那么cocos2dx会按照比例给我们做适配,不用我们做别的事情。

@H_502_6@ 在超级马里奥这个游戏里,在AppDelegate中已经调用了setDesignResolutionSize函数设置设计大小为480,320

@H_502_6@ 那么在setFrameSize不同的情况下,也不会引起图片比例不合适的情况,只是窗口大小会发生变化而已

@H_502_6@ 在480*320的情况下

@H_502_6@ 在960*640的情况下,只是界面变大了,图片没有任何的不适合

@H_502_6@ setDesignResolutionSize的参数

@H_502_6@ 第三个参数的取值范围如下:

24
enum ResolutionPolicy@H_403_105@
{@H_403_105@
// The entire application is visible in the specified area without trying to preserve the original aspect ratio.@H_403_105@
// Distortion can occur,and the application may appear stretched or compressed.@H_403_105@
kResolutionExactFit,@H_403_105@
@H_855_301@// The entire application fills the specified area,without distortion but possibly with some cropping,@H_403_105@
// while maintaining the original aspect ratio of the application.@H_403_105@
kResolutionNoBorder,@H_403_105@
// The entire application is visible in the specified area without distortion while maintaining the original@H_403_105@
// aspect ratio of the application. Borders can appear on two sides of the application.@H_403_105@
kResolutionShowAll,@H_403_105@
// The application takes the height of the design resolution size and modifies the width of the internal@H_403_105@
// canvas so that it fits the aspect ratio of the device@H_403_105@
// no distortion will occur however you must make sure your application works on different@H_403_105@
// aspect ratios@H_403_105@
kResolutionFixedHeight,@H_403_105@
// The application takes the width of the design resolution size and modifies the height of the internal@H_403_105@
// canvas so that it fits the aspect ratio of the device@H_403_105@
// no distortion will occur however you must make sure your application works on different@H_403_105@
// aspect ratios@H_403_105@
kResolutionFixedWidth,@H_403_105@
kResolutionUnKnown,@H_403_105@
};@H_403_105@
@H_502_6@ kResolutionExactFit:会靠拉伸来填满屏幕,本例来说背景图会变形来填充屏幕,因为1024:768=1.3, 480:320=1.5,宽高比不同,图片也就无法等比缩放来填满屏幕,只能变形了。

@H_502_6@ kResolutionNoBorder: 看不到黑边,实际就是宽高等比缩放,但缩放比例取宽比和高比之中大的那一个。

@H_502_6@ kResolutionShowAll:全部显示,可以理解为保证内容显示在屏幕之内,实际也是宽高等比缩放,但缩放比例取宽比和高比之中小的那一个。

猜你在找的Cocos2d-x相关文章