【问题标题】:Cocos2d for android supporting different resolutionsCocos2d for android 支持不同分辨率
【发布时间】:2013-04-07 08:00:09
【问题描述】:

我正在尝试构建一款游戏,并且想知道如何支持不同的分辨率和屏幕尺寸。对于精灵的位置,我实现了一个基本函数,它根据一定的比例设置位置,我通过从 sharedDirector 的 winSize 方法获取屏幕宽度和高度来获得。

但是这种方法没有经过测试,因为我还没有开发出一些东西来根据设备的分辨率计算精灵的缩放因子。有人可以告诉我一些方法和技巧,我可以通过这些方法和技巧正确计算精灵的缩放比例,并建议一个系统来避免精灵像素化,如果我确实应用任何这样的方法。

我在谷歌上搜索,发现 Cocos2d-x 支持不同的分辨率和大小,但我只能使用 Cocos2d。

编辑:我有点困惑,因为这是我的第一场比赛。请指出我可能犯的任何错误。

【问题讨论】:

    标签: android cocos2d-android


    【解决方案1】:

    好吧,我终于做到了,让设备显示拒绝

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

    在此基础上,对于 ldpi、mdpi、hdpi 和 xhdpi,我分别将 PTM 比率乘以 0.75、1、1.5、2.0。

    我也在相应地改变精灵的比例。对于定位,我将 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 上工作,我不知道如何在 cocos2d 中处理 android 中的多分辨率,因为我在玩游戏,所以 xml 不是在 CCLayer 上工作的 avilabel 和来自资产的所有图像文件夹你能给我一些简单的想法如何解决这个问题,我把所有的图片都放了吗?请告诉我
    • 您只需要将所有图像放入资产中,您只需根据屏幕分辨率对其进行缩放。只需从 cocos2d 中获取 winsize,根据答案我将 320X480 作为我的基本分辨率,所以我所有的资产都适合 320X480,我所做的是我得到宽度屏幕的比率和我的基本分辨率的宽度,所以这给了我一个 X 比例因子。然后我将此比例应用于每个精灵。希望你明白了。
    • 是的,我知道了,但是我要为所有图像大小创建不同的文件夹?
    • 另外我想补充一下,如果您正在设置您的位置并且希望每个分辨率都相同,然后将您的位置乘以您计算的比例 x 和比例 y 因子。
    • somesprite.setPosition(80fscaleX,250fscaleY); 是什么?您如何计算这些值(80f 和 250f)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多