现在的位置: 首页 > 综合 > 正文

最新 cocos2d-x自适应android不同分辨率,使用多套资源

2018年02月16日 ⁄ 综合 ⁄ 共 3713字 ⁄ 字号 评论关闭

虽然 cocos2d-x自带了三种模式来适应屏幕,但还是存在一些问题,会产生黑边,所以还是需要多套资源来适应屏幕。

这里我选择了四种分辨率9:16 3:5 2:3 3:4,在resource资源目录下新建了四个文件夹放置多套背景图片,在AppDelegate中添加如下函数调用即可。

  1. void AppDelegate::ScreenAdaptive()  
  2. {  
  3.     CCSize szFrame = CCEGLView::sharedOpenGLView()->getFrameSize();  
  4.     float Proportion = szFrame.width/szFrame.height;  
  5.     const int num = 4;  
  6.     float diff[num]={fabs(Proportion-(9.0/16)),  
  7.         fabs(Proportion-(3.0/5)),fabs(Proportion-(2.0/3)),fabs(Proportion-(3.0/4))};  
  8.     int yy = -1;  
  9.     for (int i=0;i<num;i++)  
  10.         if((diff[i] >-0.000001) && (diff[i] < 0.000001))  
  11.         {  
  12.             yy = i;  
  13.             break;  
  14.         }  
  15.     if (yy == -1)  
  16.     {  
  17.         int min = diff[0];  
  18.         yy = 0;  
  19.         for (int i=1;i<num;i++)  
  20.             if (diff[i]<min)  
  21.             {  
  22.                 min = diff[i];  
  23.                 yy = i;  
  24.             }  
  25.     }  
  26.     switch(yy)  
  27.     {  
  28.     case 0:  
  29.         {  
  30.             std::vector<string> searchPaths;  
  31.             searchPaths.push_back("9x16");  
  32.             CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
  33.             CCEGLView::sharedOpenGLView()->setDesignResolutionSize(648,1152, kResolutionShowAll);  
  34.             break;  
  35.         }  
  36.     case 1:  
  37.         {  
  38.             std::vector<string> searchPaths;  
  39.             searchPaths.push_back("3x5");  
  40.             CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
  41.             CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,1040, kResolutionShowAll);  
  42.             break;  
  43.         }  
  44.     case 2:  
  45.         {  
  46.             std::vector<string> searchPaths;  
  47.             searchPaths.push_back("2x3");  
  48.             CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
  49.             CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640,960, kResolutionShowAll);  
  50.             break;  
  51.         }  
  52.     case 3:  
  53.         {  
  54.             std::vector<string> searchPaths;  
  55.             searchPaths.push_back("3x4");  
  56.             CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);  
  57.             CCEGLView::sharedOpenGLView()->setDesignResolutionSize(624,832, kResolutionShowAll);  
  58.             break;  
  59.         }  
  60.     }  
  61. }  

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

  1. /**  
  2.  *  Sets the array of search paths. 
  3.  *  
  4.  *  You can use this array to modify the search path of the resources. 
  5.  *  If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array. 
  6.  * 
  7.  *  @note This method could access relative path and absolute path. 
  8.  *        If the relative path was passed to the vector, CCFileUtils will add the default resource directory before the relative path. 
  9.  *        For instance: 
  10.  *         On Android, the default resource root path is "assets/". 
  11.  *         If "/mnt/sdcard/" and "resources-large" were set to the search paths vector, 
  12.  *         "resources-large" will be converted to "assets/resources-large" since it was a relative path. 
  13.  * 
  14.  *  @param searchPaths The array contains search paths. 
  15.  *  @see fullPathForFilename(const char*) 
  16.  *  @since v2.1 
  17.  */  


  1. // set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example"   
  2. vector<string> searchPaths;  
  3. searchPaths.push_back("/mnt/sd/example");  
  4. searchPaths.push_back("/data/data/org.cocos2dx.example");  
  5. CCFileUtils::setSearchPaths(searchPaths);    
  6. // 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  
  7. // if not found, engine will find "1.png" in Resources/ (this path is platform dependent)  


  1. CCSprite *pSprite = CCSprite::create("1.png"); 

抱歉!评论已关闭.