【问题标题】:auto resizing in cocos2dx在 cocos2dx 中自动调整大小
【发布时间】:2012-07-27 07:10:57
【问题描述】:

我已经使用 cocos2dx 为尺寸为 480*320 的设备制作了 android 版本,它工作正常,但是当我将相同的版本放入另一台尺寸为 640*480 的 android 设备中时strong> 发生缩放问题....

我正在使用以下代码自动调整大小,但它不起作用:

AppDelegate app;
CCEGLView& eglView = CCEGLView::sharedOpenGLView();
eglView.setViewName("Hello Lua");
eglView.setFrameSize(480, 320);

// set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line.
// eglView.setDesignResolutionSize(480, 320);

【问题讨论】:

  • 您遇到的扩展问题到底是什么?截图会有所帮助。

标签: android ios cocos2d-iphone cocos2d-x


【解决方案1】:

首先,您粘贴的代码来自 main.cpp 文件,当您为 android 或 windows phone 或 ios 编译应用程序时,该文件不起作用。该文件用于win32项目。

现在, 对于应用程序的自动缩放,AppDelegate.cpp 中的函数应该设置设计分辨率以及策略。我使用 ExactFit 策略是因为它可以将应用程序拉伸以填充整个屏幕区域,并且它适用于 Android、Windows、iOS 一切(我已经开发了应用程序并进行了测试):

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    // turn on display FPS
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(1200, 720, kResolutionExactFit);
    pDirector->setDisplayStats(false);

    pDirector->setAnimationInterval(1.0 / 60);

    CCScene *pScene = HelloWorld::scene();

    pDirector->runWithScene(pScene);
    return true;
}

您还应该看看this Detailed explanation of multi-resolution support 以更好地理解它。

【讨论】:

  • 这不适用于 ipad 设备,因为 ipad 设备的分辨率为 768x1024,这会使某些东西在 Y 轴上拉伸。您应该设置比例以避免拉伸屏幕。
【解决方案2】:

如果您不注意拉伸外观,请执行此操作。它适用于所有设备。

pDirector->setOpenGLView(pEGLView);

CCSize frameSize = pDirector->getOpenGLView()->getFrameSize();
CCLog("%.2f and %.2f is Frame size\n",frameSize.width,frameSize.height);
pEGLView->setDesignResolutionSize(960, 640, kResolutionExactFit);

注意:以上设置是针对横向的,如果您需要纵向设置,只需反转 pEGLView->setDesignResolutionSize(640, 960, kResolutionExactFit) 等值即可

这里要理解的是,您将在 640X960 坐标上设计的所有内容,并且 kResolutionExactFit 会将这些内容修复为适合屏幕。要更轻松地做到这一点,您应该使用 Coocos Builder http://cocosbuilder.com/ 并在 640X960 的自定义布局上设计所有内容

我推荐双分辨率 (640X960),因为这样质量不会降低,并且在所有类型的设备上看起来都很清晰,但是拉伸外观的缺点会存在。

如果您想以多种方式考虑多种分辨率,那么您必须制作多个图形和多个布局,再次,Cocos Builder 将是最好的方法。

即,对于 iPhone 5,它会有所不同,对于 iPad 和标准 480X320,它会有所不同。为此,您可以检查帧大小以选择相应的视图。

CCSize frameSize = pDirector->getOpenGLView()->getFrameSize();

【讨论】:

    【解决方案3】:

    您应该在每一侧都经历一些切割。这样做是因为兼容模式与 480x320 的屏幕分辨率匹配的最佳方式。其他选项是保留部分高度或拉伸图像(以牺牲一些性能为代价(?))。

    第一个可以通过CCEGLView::create 方法轻松完成,您可以将比例因子公式中的 MIN 更改为 MAX 并完成,但这可能会破坏您 sprite 的所有 y 位置:P

    【讨论】:

      【解决方案4】:
      bool AppDelegate::applicationDidFinishLaunching()
      {
          // initialize director
          CCDirector* pDirector = CCDirector::sharedDirector();
          CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
      
          pDirector->setOpenGLView(pEGLView);
      
          // Set the design resolution
          // iPad size
          CCPoint res = CCPoint(768,1024);
          pEGLView->setDesignResolutionSize(res.x,
                                            res.y,
                                            kResolutionNoBorder);
        .....
      }
      

      【讨论】:

        【解决方案5】:

        检查分辨率并更改屏幕尺寸: (本例为人像游戏)

        typedef struct tagResource
        {
           cocos2d::Size size;
           char directory[100];
        }Resource;
        
        std::vector<std::string> searchPath;
        static Resource designResource =  { cocos2d::Size(768, 1024),  "ipad"};
        static Resource smallResource  =  { cocos2d::Size(320, 480),   "iphone"};
        static Resource galax7Resource =  { cocos2d::Size(600, 1024),  "gt_7p"  };
        static Resource mediumResource =  { cocos2d::Size(768, 1024),  "ipad"};
        static Resource galax10Resource =  { cocos2d::Size(800, 1280),  "gt_10p"};
        static Resource fullHDResource  =  { cocos2d::Size(1080, 1920), "fullhd"};
        static Resource largeResource  =  { cocos2d::Size(1536, 2048), "ipadhd"};
        auto director = Director::getInstance();
        auto glview = director->getOpenGLView();
        glview->setDesignResolutionSize(designResource.size.width, designResource.size.height, ResolutionPolicy::EXACT_FIT);
        Size frameSize = glview->getFrameSize();
        Resource realResource;
        // if the frame's height is larger than the height of medium size.
        if (frameSize.height > fullHDResource.size.height) {
            realResource = largeResource;
            CCLOG("largeResource");
        } else if (frameSize.height > galax10Resource.size.height) {
            realResource = fullHDResource;
            CCLOG("fullHDResource");
        } else if (frameSize.height > mediumResource.size.height) {
            realResource = galax10Resource;
            CCLOG("galax10Resource");
        } else if (frameSize.height > smallResource.size.height) {
            if(frameSize.width > galax7Resource.size.width){
                realResource = mediumResource;
                CCLOG("mediumResource");
            }else {
                realResource = galax7Resource;
                CCLOG("galax7Resource");
            }
        } else {
            realResource = smallResource;
            CCLOG("smallResource");
        }
        director->setContentScaleFactor(MIN(realResource.size.height/designResource.size.height, realResource.size.width/designResource.size.width));
        searchPath.push_back(realResource.directory);
            auto fileUtils = FileUtils::getInstance();
        fileUtils->setSearchPaths(searchPath);
        

        我使用的是 cocos2dx V3,但 V2 的想法是一样的。

        【讨论】:

          【解决方案6】:

          我在我的 3match 游戏中使用了以下分辨率政策 在 AppDelegate.cpp 中

          #define IS_LANDSCAPE
          
          #ifdef IS_LANDSCAPE
          static cocos2d::Size designResolutionSize = cocos2d::Size(1136,768);
          #else
          static cocos2d::Size designResolutionSize = cocos2d::Size(768,1136);
          #endif
          
          //==========================
          bool AppDelegate::applicationDidFinishLaunching() {
          
              // initialize director
              auto director = Director::getInstance();
              auto glview = director->getOpenGLView();
              if(!glview) {
                  glview = GLViewImpl::create("My Game");
                  director->setOpenGLView(glview);
              }
          
              // turn on display FPS
              director->setDisplayStats(false);
          
              // set FPS. the default value is 1.0/60 if you don't call this
              director->setAnimationInterval(1.0 / 60);
          
              // Set the design resolution
               glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
          
              register_all_packages();
          
              // create a scene. it's an autorelease object
              auto scene =loading::createScene();
          
              // run
              director->runWithScene(scene);
          
              return true;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-04-29
            • 2021-11-25
            • 2018-02-06
            • 2015-03-13
            • 2013-12-16
            • 2015-06-16
            • 2011-04-02
            • 2013-07-16
            相关资源
            最近更新 更多