android - Cocos2d for android 支持不同的分辨率

标签 android cocos2d-android

我正在尝试制作一款游戏,想知道如何支持不同的分辨率和屏幕尺寸。对于 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/

相关文章:

android - 与cocos2d android中的其他对象发生碰撞时如何删除对象( Sprite )?

android - cocos2d-android:如何显示分数

java - 服务抛出未知主机异常 Android 应用程序

android - 入队 onResponse 方法在 android 改造中返回空的 response.body()

android - 数据绑定(bind)失败, "couldn' 猜猜”

java - 如何在java中使用Cocos2d创建动画?

android - iOS开发后学习使用Cocos2D for android

cocos2d-x - Cocos2dX, Assets 在构建或运行时被删除(Eclipse Juno,Android C++ 项目)

Android Kotlin 子级 onClick 会阻塞父级 OnTouch

android - 如果选中一个特定的复选框,则执行某些操作,如果单击任何其他复选框,则执行其他操作