如何使 TMXTiledMap 具有响应性?

How to make TMXTiledMap responsive?

我的游戏是一款基于 2D 汽车的游戏,有一张笔直的无限地图,我终于能够在其中添加一些随机障碍物。小车只能停3个位置,一切正常

关键是我最近注意到它没有响应,并试图通过在 AppDelegate.cpp:

glview->setDesignResolutionSize(1024.0, 600.0, kResolutionFixedWidth);

我尝试使用 kResolutionFixedWidthkResolutionFixedHeight 和所有其他 5 个变量,但我只有黑线沿着屏幕和你能想象到的每一个屏幕故障-.-'

我知道我需要手动调整 TMXTiledMap 的大小,因为图块的性质(我用 Tiled 做到了),但我不知道如何面对这个问题。

请注意,我目前正在为 1024x600 Android 设备开发,但我希望至少支持平板电脑和智能手机最常见的分辨率。

可能有 2 个您要使用的解析策略。

如果您使用 无边框 那么您应该看不到任何黑条,但引擎会裁剪您的设计分辨率,因此您不想放置 UI 在角落里,或者你会想要使用 Visible Origin 和 Visible Size 来计算位置。

如果您使用 Exact Fit,您应该将设计分辨率设置为设备的精确尺寸,然后您负责正确定位和缩放所有内容以避免失真。

如果您看到黑条,则需要根据您的政策和设计分辨率选择来缩放您的作品。

您是否已阅读此 wiki 页面? http://www.cocos2d-x.org/wiki/Multi_resolution_support

这是我们为其中一款游戏所做的:

auto director = Director::getInstance();
auto glview = director->getOpenGLView();

float contentScaleFactor = 1.f;

// Set the design resolution
Size frameSize = glview->getFrameSize();
Size designSize = glview->getDesignResolutionSize();
CCLOG("defaults:");
CCLOG("framesize = {%f,%f}", frameSize.width, frameSize.height);
CCLOG("visibleSize = {%f,%f}", glview->getVisibleSize().width, glview->getVisibleSize().height);
CCLOG("designSize = {%f,%f}", designSize.width, designSize.height);
CCLOG("contentscalefactor = %f", director->getContentScaleFactor());

Vec2 origin = director->getVisibleOrigin();
CCLOG("visibleSize = %s", CStrFromSize(director->getVisibleSize()));
CCLOG("origin = {%f,%f}", origin.x, origin.y);

// Retina? 
contentScaleFactor = director->getContentScaleFactor();
float designWidth = frameSize.width / contentScaleFactor;
float designHeight = frameSize.height / contentScaleFactor;
CCLOG("contentScale = %f, designWidth/Height = {%f,%f}", contentScaleFactor, designWidth, designHeight);

glview->setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy::EXACT_FIT);

// we designed the game for 480x320 (hence the divisors)
// used to scale full screen backgrounds
float fullWidthScaleFactor = designWidth/480.f;
// used to scale up most UI
float largeScaleFactor = floorf(designHeight/320.f);
// round to closest HALF step (1.0,1.5,2.0,2.5,3.0,etc)
// used for scaling UI where pixel art is affected by .1 scales
float largeScaleFactorExact = floorf(designHeight * 2.f / 320.f) * 0.5f;
// used to scale up UI that must be touchable (larger on high desnsity)
float largeScaleFactorUI = STROUND(designHeight / 320.f);

// this forces minimum of 1x scale (we should just not support these devices)
float scaleFitAll = designWidth > designHeight ? designHeight/320.f : designWidth/480.f;
if(largeScaleFactor < 1.f)
    largeScaleFactor = scaleFitAll;
if(largeScaleFactorExact < 1.f)
    largeScaleFactorExact = scaleFitAll;
if(largeScaleFactorUI < 1.f)
    largeScaleFactorUI = scaleFitAll;