简体   繁体   English

Android libGDX关于精灵大小

[英]Android libGDX about sprite sizes

I'm making game with libGDX in Android and I have some question about sprite's sizes. 我正在使用Android中的libGDX进行游戏,并且我对子画面的大小有疑问。 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. 我想使用与景观场景中的屏幕背景相同的精灵,并且我希望该背景与每一个16:9设备完全匹配。

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. 我的问题是我不知道该图片的最佳尺寸是什么,因为如果我制作1920x1080的图片,它将占用大量内存并损害游戏性能,或者如果我制作的图像很少,则在某些情况下看起来会很糟糕设备。 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 我将选择16:9的相机尺寸,也许是1024 * 576(或其他适合您的尺寸),还可以使用.jpg格式(而不是.png格式)来节省内存,这样可以节省很多空间

In Libgdx you don't have to worry about different device sizes as long as you are using a camera or orthographic camera. 在Libgdx中,您不必担心设备大小不同,只要使用相机或正交相机即可。 I will make sure the screen fits properly even on non 16:9 devices with some stretching 即使在非16:9的设备上进行拉伸,我也将确保屏幕正常显示

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 PS:您可以做一些与您想要的略有不同的操作,但这可以节省大量内存,就好像您将Sprite Sheet制成.jpg一样,图像的透明度也会丢失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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