Par*_*kar 6 android cocos2d-android
我正在尝试建立一个游戏,并想知道如何支持不同的分辨率和屏幕尺寸.对于精灵的位置,我已经实现了一个基本功能,它根据一定比例设置位置,这是通过从sharedDirector的winSize方法获取屏幕宽度和高度得到的.
但是这种方法没有经过测试,因为我还没有开发一些东西来计算精灵的比例因子,具体取决于设备的分辨率.有人可以告诉我一些方法和提示,通过它我可以正确计算精灵的缩放比例,并建议一个系统,以避免精灵的像素化,如果我应用任何这样的方法.
我在Google上搜索并发现Cocos2d-x支持不同的分辨率和大小但我必须只使用Cocos2d.
编辑:我有点困惑,因为这是我的第一场比赛.请指出我可能犯的任何错误.
好吧,我终于做了这个,让设备显示为denitiy
getResources().getResources().getDisplayMetrics().densityDpi
Run Code Online (Sandbox Code Playgroud)
在此基础上,我分别将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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2421 次 |
| 最近记录: |