cocos2d-x二(自适应屏幕分辨率)

在android中,适应各种分辨率是个很头疼的事,特别是对于游戏来说……网上教程介绍得已经很详细了,下面是个人认为写得比较全的文章链接:

1.http://codingnow.cn/cocos2d-x/975.html 或者http://blog.csdn.net/gg137608987/article/details/8302699

2.http://article.ityran.com/archives/3762

其实对于我来说,最简单的屏幕适配就是使用CCEGLView::sharedOpenGLView()->setDesignResolutionSize(width,Height, kResolutionShowNoBorder);在我看来,setDesignResolutionSize()就是根据对应图片资源尺寸,也就是你设计的界面元素图片最适合的显示分辨率,按最适当的比例显示到屏幕上。这样的话,除了让其它界面元素都使用相对坐标(应该没几个会使用绝对坐标的吧……),就没多少需要做的了。

在最近更新的版本里,已经没有了setResourceDirectory,取而代之的是setSearchPaths,以下是关于它的描述:

    /** 
     *  Sets the array of search paths.
     * 
     *  You can use this array to modify the search path of the resources.
     *  If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array.
     *
     *  @note This method could access relative path and absolute path.
     *        If the relative path was passed to the vector, CCFileUtils will add the default resource directory before the relative path.
     *        For instance:
     *        	On Android, the default resource root path is "assets/".
     *        	If "/mnt/sdcard/" and "resources-large" were set to the search paths vector,
     *        	"resources-large" will be converted to "assets/resources-large" since it was a relative path.
     *
     *  @param searchPaths The array contains search paths.
     *  @see fullPathForFilename(const char*)
     *  @since v2.1
     */
如果想显示比较完整清晰,那么针对不同分辨率设计对应尺寸的图片将是必要的,setSearchPaths可以用来设定引用资源的路径,根据不同分辨率设定引用对应的图片资源即可保证完美清晰的显示。下面是cocosWiki上的使用示例:
// set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example" 
vector<string> searchPaths;
searchPaths.push_back("/mnt/sd/example");
searchPaths.push_back("/data/data/org.cocos2dx.example");
CCFileUtils::setSearchPaths(searchPaths);  
// engine will find "1.png" in /mnt/sd/example, if there it is not found, then engine will find "1.png" in /data/data/org.cocos2dx.example
// if not found, engine will find "1.png" in Resources/ (this path is platform dependent)
CCSprite *pSprite = CCSprite::create("1.png");
下面还有些文件命名的注意事项:
  • Do not use -hd, -ipad & -ipadhd suffix(后缀) as the "file" name field in .fnt file
  • Do not use -hd, -ipad & -ipadhd suffix(后缀) as the "textureFilename" name field in .plist file
  • Do not use -hd, -ipad & -ipadhd suffix(后缀) as the "image source" name field in .tmx file
  • You should set shared resource's scale property to achieve the same display size on both Retina & Non-Retina devices, but particles are exceptional.
  • Use centralized strategy as you can
原文章还包含了setSearchResolutionsOrder的使用示例,这里是示例引用的原链接:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Mechanism_of_loading_resources