简体   繁体   中英

Android libGDX about sprite sizes

I'm making game with libGDX in Android and I have some question about sprite's sizes. I want to use same sprite as background of screen in landscape scene, and I want to this background perfectly match every 16:9 device.

My problem is that I don't know what is optimal size of that picture, because if I make 1920x1080 picture, it will use a lot of memory and damage performance of game or I if I made little image, it will look bad on some devices. What is the practice in cases like this?

Thanks in advice.

Here is how would i approach the situation. I will choose my camera size of the 16:9 maybe 1024*576 (or whatever suits you) also to save the memory use .jpg format instead of .png, it will save lot of space

In Libgdx you don't have to worry about different device sizes as long as you are using a camera or orthographic camera. I will make sure the screen fits properly even on non 16:9 devices with some stretching

here is some code snippet

public class LoadScreen extends Screen
{

   Game game;
   SpriteBatch batcher;
   OrthographicCamera cam;
   AssetManager manager;
   public static BitmapFont font;
   private String dir="data/";

public LoadScreen(Game game,SpriteBatch batcher)
 {
    super(game);
    this.game=game;
    this.batcher=batcher;
    cam = new OrthographicCamera(GameConstants.CAMERA_WIDTH, GameConstants.CAMERA_HEIGHT);
    cam.position.set(GameConstants.CAMERA_WIDTH / 2, GameConstants.CAMERA_HEIGHT / 2,0);
    manager=new AssetManager();
    font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);
    loadData();
}
private void loadData()
{
    manager.load(dir+"monkey",TextureAtlas.class);
    manager.load(dir+"levelBg.jpg",Texture.class);
    manager.load(dir+"gamescreen.jpg",Texture.class);
    manager.load(dir+"apple.png",Texture.class);
}
}

And in the asset class

public class Assets {

public static AssetManager manager;
public static TextureAtlas atlas;
private static String dir = "data/";
public static Sprite levelBg;

public static void load(AssetManager manager)
{
    Assets.manager = manager;
    loadAtlas();
    loadTextures();
    loadImageFromAtlas();
    loadFont();
}
private static void loadAtlas()
{
    atlas = manager.get(dir + "monkey", TextureAtlas.class);
}

private static void loadTextures()
{
    levelBg = new Sprite(manager.get(dir + "levelBg.jpg", Texture.class));
    gameBg = new Sprite(manager.get(dir + "gamescreen.jpg", Texture.class));
    apple = new Sprite(manager.get(dir + "apple.png", Texture.class));
}
}

PS You can do things slightly differntly like u want but this saves a lot of memory as if u make sprite sheet as .jpg the transparency from images will be lost

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM