我正在尝试制作一款游戏,想知道如何支持不同的分辨率和屏幕尺寸。对于 Sprite 的位置,我实现了一个基本功能,它根据一定的比例设置位置,我通过从 sharedDirector 的 winSize 方法获取屏幕宽度和高度来获得。
但是这种方法没有经过测试,因为我还没有开发出一些东西来根据设备的分辨率计算 Sprite 的比例因子。有人可以告诉我一些方法和技巧,我可以通过这些方法和技巧正确计算 Sprite 的缩放比例,并建议一个系统来避免 Sprite 像素化(如果我确实应用了任何此类方法)。
我在 Google 上搜索,发现 Cocos2d-x 支持不同的分辨率和尺寸,但我只能使用 Cocos2d。
编辑:我有点困惑,因为这是我的第一场比赛。请指出我可能犯的任何错误。
最佳答案
好吧,我终于通过让设备显示密度做到了这一点
getResources().getResources().getDisplayMetrics().densityDpi
基于它,我将 PTM 比率分别乘以 0.75、1、1.5、2.0,分别用于 ldpi、mdpi、hdpi 和 xhdpi。
我也在相应地改变 Sprite 的比例。为了定位,我将 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);
}
}
关于android - Cocos2d for android 支持不同的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16011216/