在cocos2dx中自动调整大小

时间:2012-07-27 07:10:57

标签: android ios cocos2d-iphone cocos2d-x

我已经使用cocos2dx为大小 480 * 320 的设备制作了android版本并且它工作正常但是当我将相同的版本放在另一个尺寸 640 * 480的Android设备上时缩放问题发生....

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

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);

6 个答案:

答案 0 :(得分:4)

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

现在, 对于app的自动缩放,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以更好地理解它。

答案 1 :(得分:3)

如果你没有意识到拉伸的外观,那么这样做。它适用于所有设备。

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();

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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);
  .....
}

答案 4 :(得分:0)

检查分辨率并更改屏幕尺寸: (这个例子是一个肖像游戏)

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的想法是一样的。

答案 5 :(得分:0)

我在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;
}