6

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.

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

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

ManishSB
  • 767
  • 9
  • 27
Parvaz Bhaskar
  • 1,367
  • 9
  • 29

1 Answers1

9

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

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

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.

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.

EDIT: 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);
    }
}
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
  • Hi I Also working On Cocos2d-android i didnt get proper idea how to handle multiresolution in android in cocos2d because i am wrking on Game so xml is nt avilabel wrking on CCLayer And ALl Image Coming From the Assets folder Can You please Give me some Brief idea How to fix this wher i put all images?please tell me – ishu Jun 08 '13 at 13:38
  • you'll have to put all your images in assets only,you just have to scale it according to the screen resolution. Just get the winsize from cocos2d, according to the answer I've kept 320X480 as my base resolution,so all my assets are fit for 320X480,what I do is i get the ratio of width screen and the width of my base resolution, so this gives me an X scale factor. I then apply this scale to every sprite. Hope you got it. – Parvaz Bhaskar Jun 10 '13 at 04:04
  • ya i got it but i have tu create different diffrent folder for all image size? – ishu Jun 10 '13 at 11:13
  • Also I'd like to add if you're setting your positions and want to be same for every resolution then multiply your position with scale x and scale y factor that you calculated. – Parvaz Bhaskar Jun 10 '13 at 11:55
  • What is somesprite.setPosition(80f*scaleX,250f*scaleY);? How do you calculate those values (80f & 250f)? – nano Oct 24 '14 at 16:49