Cocos2d for android, supporting different resolutions

I am trying to create a game and wonder how different resolutions and screen sizes can be supported. For the sprite position, I implemented a basic function that sets the position according to the specific ratio that I get, getting the width and height of the screen from the winDize sharedDirector method.

But this approach has not been tested, since I have yet to develop something to calculate the scale factor for sprites depending on the resolution of the device. Can someone advise me on some method and tips on which I can correctly calculate the scaling of the sprite and suggest a system to avoid pixelation of sprites if I use any such method.

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

EDIT: I'm a little confused since this is my first game. Please indicate any errors that I may have made.

+6
source share
1 answer

Ok, I finally did this by getting the denitiy device mapping as

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

and based on this, the multiplier is multiplied by a PTM coefficient of 0.75,1,1,5,2,0 for ldpi, mdpi, hdpi and xhdpi, respectively.

I also scale the sprites. And for positioning, I saved 320X480 as a base, and then multiplied it with the ratio of my current x and y in pixels with basic pixels.

EDIT: adding code for a 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); } } 
+9
source

All Articles