cocos2d的为Android支持不同分辨率 [英] Cocos2d for android supporting different resolutions

查看:131
本文介绍了cocos2d的为Android支持不同分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个游戏,想知道如何将一去支持不同的分辨率和屏幕尺寸。对于精灵的位置,我已经实现了一个基本功能,设置按一定的比例,这是我获得通过获得来自sharedDirector的使用winsize方法的屏幕宽度和高度的位置。

I am trying to build a game and was wondering how would one go about supporting different resolution and screen sizes. For position of sprite I've implemented a basic function which sets the position according to a certain ratio, which I get by getting the screen width and height from sharedDirector's winSize method.

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

But this approach is not tested as I have yet to develop something to calculate the scaling factor for sprites depending upon the resolution of device. Can somebody advise me some method and tips by which I can correctly calculate the scaling of sprite and suggest a system to avoid the pixelation of sprites if I do apply any such method.

我搜索谷歌和发现的Cocos2D-X支持不同的分辨率和尺寸,但我一定要使用的cocos2d只。

I searched on Google and found that Cocos2d-x supports different resolutions and sizes but I am bound to use Cocos2d only.

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

I am bit confused as this is my first game. Please point out any mistakes that I may have made.

推荐答案

好吧,我终于通过获取设备显示denitiy像这样做

Okay I finally did this by getting the device display denitiy like

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

和在此基础上,我通过PTM的比例分别为LDPI,MDPI,华电国际,和xhdpi乘以0.75,1,1.5,2.0。

and based on it I am multiplying by PTM ratio by 0.75,1,1.5,2.0 for ldpi,mdpi,hdpi,and xhdpi respectively.

我也改变精灵的规模相应。而对于定位我已经把320X480作为我的基地,然后乘与我目前x的像素与我的基地像素的比例和y。

I am also changing the scale of sprites accordingly. And for positioning I've kept 320X480 as my base and then multiplying that with a ratio of my current x and y in pixels with my base pixels.

编辑:添加一些code为了更好地理解:

Adding some code for better understanding:

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支持不同分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆