cocos2d-x在开发游戏时 常常会遇到手机屏幕分辨率不统一,导致加载到不同分辨率下显示出游戏效果出现不一致的问题。
在cocos2d-x 2.0版本以后可以使用
virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy);
解决
具体方法为:
1.首先确定一个你开发环境和图片资源所在的分辨率。也就是你在开发时,在确定的该分辨率下场景布局是正确的。(此处以iphone4s手机分辨率960*640为例)
2.在开发完成后在 AppDelegate.cpp 中
bool AppDelegate::applicationDidFinishLaunching();
接口当中
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
这行代码下面添加上这一句:CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionExactFit);
如图:
此时在不同分辨率下 场景布局和排版会适应屏幕大小 不过 kResolutionExactFit 参数采取的是根据屏幕分辨率相对于960*640的图片或者控件的拉伸适应 也就是说如果分辨率过大,相关图片和精灵也会相应的变大,有可能会出现图片模糊。
该参数还可以传入其他几种模式:
enum ResolutionPolicy
{
// The entire application is visible in the specified area without trying to preserve the original aspect ratio.
// Distortion can occur, and the application may appear stretched or compressed.
kResolutionExactFit,
// The entire application fills the specified area, without distortion but possibly with some cropping,
// while maintaining the original aspect ratio of the application.
kResolutionNoBorder,
// The entire application is visible in the specified area without distortion while maintaining the original
// aspect ratio of the application. Borders can appear on two sides of the application.
kResolutionShowAll,
kResolutionUnKnown,
};