安卓平台的Cocos2d支持不同分辨率

6
我正在尝试制作一个游戏,想知道如何支持不同的分辨率和屏幕尺寸。对于精灵的位置,我实现了一个基本函数,根据 sharedDirector 的 winSize 方法获取屏幕宽度和高度来设置位置比例。但是这种方法没有经过测试,因为我还没有开发出可以根据设备分辨率计算精灵缩放因子的东西。有人能够为我提供一些方法和技巧吗?请正确计算精灵的缩放,并建议一种系统,以避免在应用任何此类方法时出现像素化问题。
我在谷歌上搜索到 Cocos2d-x 可以支持不同的分辨率和尺寸,但我只能使用 Cocos2d。
编辑:这是我的第一个游戏,我有点困惑,请指出我可能犯的任何错误。
1个回答

9

好的,我最终通过获取设备显示密度来完成了这个操作,类似于

getResources().getResources().getDisplayMetrics().densityDpi

根据它,我将PTM比率分别乘以0.75、1、1.5和2.0,用于ldpi、mdpi、hdpi和xhdpi。同时,我也相应地改变了精灵的比例尺。对于定位,我将320X480作为基础,并将当前像素的x和y与基础像素相乘得到比率。
编辑:添加一些代码以便更好地理解:
public class MainLayer extends CCLayer()
{
CGsize size; //this is where we hold the size of current display
float scaleX,scaleY;//these are the ratios that we need to compute
public MainLayer()
{
  size = CCDirector.sharedDirector().winSize();
  scaleX = size.width/480f;//assuming that all my assets are available for a 320X480(landscape) resolution;
  scaleY = size.height/320f;

  CCSprite somesprite = CCSprite.sprite("some.png");
  //if you want to set scale without maintaining the aspect ratio of Sprite

  somesprite.setScaleX(scaleX);
  somesprite.setScaleY(scaleY);
  //to set position that is same for every resolution
  somesprite.setPosition(80f*scaleX,250f*scaleY);//these positions are according to 320X480 resolution.
  //if you want to maintain the aspect ratio Sprite then instead up above scale like this
  somesprite.setScale(aspect_Scale(somesprite,scaleX,scaleY));

}

public float aspect_Scale(CCSprite sprite, float scaleX , float scaleY)
    {
        float sourcewidth = sprite.getContentSize().width;
        float sourceheight = sprite.getContentSize().height;

        float targetwidth = sourcewidth*scaleX;
        float targetheight = sourceheight*scaleY;
        float scalex = (float)targetwidth/sourcewidth;
        float scaley = (float)targetheight/sourceheight;


        return Math.min(scalex,scaley);
    }
}

嗨,我也在使用Cocos2d-android进行开发,但是我不知道如何在Android中处理多分辨率问题,因为我正在开发游戏,所以XML文件不可用,我正在使用CCLayer和所有图像都来自Assets文件夹。你能否给我一些简要的想法,如何解决这个问题,我应该把所有图像放在哪里?请告诉我。 - ishu
你需要把所有的图片都放在 assets 文件夹中,并根据屏幕分辨率进行缩放。只需从 cocos2d 中获取 winsize,我选择将 320X480 作为基本分辨率,因此所有的资源都适用于 320X480 分辨率。我会获取屏幕宽度与基本分辨率宽度之比,这样就能得到一个 X 缩放因子。接着,我会将该缩放因子应用到每个精灵上。希望你明白了。 - Parvaz Bhaskar
我明白了,但是我需要为每个图像大小创建不同的文件夹吗? - ishu
另外,如果您正在设置位置并且希望每个分辨率都相同,则可以将您的位置乘以比例因子scale x和scale y。 - Parvaz Bhaskar
somesprite.setPosition(80fscaleX,250fscaleY)是什么?你如何计算这些值(80f和250f)? - nano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接