简体   繁体   English

LibGDX从哈希图旋转精灵

[英]LibGDX Rotating Sprites from a Hashmap

I'm pretty new to libGDX and java in general so I've been following a plethora of tutorials. 一般来说,我对libGDX和Java还是很陌生,所以我一直在关注大量教程。 so FEEL FREE TO CORRECT ME AT EVERY TURN! 请随时纠正我! (yes even in code inefficiencies as well!!) (即使在代码效率低下也可以!!)

I currently have the issue of rotating all of the sprites of a single type instead of just a single sprite. 我目前遇到的问题是旋转所有单一类型的精灵,而不是仅一个精灵。

I'll show you how I mean: 我会告诉你我的意思:

libGDX sprite rotation issue libGDX精灵旋转问题

Here are the specifics: 具体细节如下:

  1. There are 3 different size of sprites. 精灵共有3种大小。

  2. I use a hashmap to store the 3 sprites so I don't have to Sprite sprite = new Sprite(); 我使用哈希表存储3个Sprite sprite = new Sprite();因此不必Sprite sprite = new Sprite(); every time (supposedly it's a heavy task) 每次(据说这是一个繁重的任务)

  3. I create a sprite by referencing the one from the hashmap(I think?) 我通过引用哈希图中的一个来创建一个精灵(我想?)

  4. PROBLEM: When I tell a specific sprite to rotate, it rotates each other sprite of its size. 问题:当我告诉特定的精灵旋转时,它会彼此旋转其大小的精灵。

I have a suspicion that I'm rotating the hashmap reference... If that's the right way to say it. 我怀疑我正在旋转哈希图引用...如果这是正确的说法。

This is the process I've been using: 这是我一直在使用的过程:

hashpmap: hashpmap:

final HashMap<String, Sprite> spriteMap = new HashMap<String, Sprite>();

texture atlas: 纹理图集:

spriteAtlas = new TextureAtlas("sprites.txt");

fill hashmap with regions from texture atlas: 用纹理图集的区域填充哈希图:

private void init spriteMap() {
    Array<AtlasRegion> regions = spriteAtlas.getRegions();

    for (int i = 0; i < regions.size; i++) {
        AtlasRegion region = regions.get(i);
        Sprite sprite = spriteAtlas.createSprite(region.name);

        float width = sprite.getWidth() / SCALE;
        float height = sprite.getHeight() / SCALE;

        sprite.setSize(width, height);
        sprite.setOrigin(0, 0);
        sprite.scale(1f);
        spriteMap.put(region.name, sprite);
    }
}

create sprite "instance" from hashmap: 从哈希表创建精灵“实例”:

private void createNewSprite() {
    Sprite sprite;
    sprite = spriteMap.get(name);
    sprite.setPosition(x, y);
    sprite.rotate(rotation);
    spriteArray.add(sprite);
}

Maybe I'm not actually extrapolating the sprite from the hashmap well enough? 也许我实际上没有从哈希图中充分推断出精灵? Let me know if you need anything else to figure out this conundrum. 让我知道您是否还有其他需要解决的难题。

Thank you so much! 非常感谢!

You have only created a single sprite for each region name, referencing it multiple times in spriteArray . 您只为每个区域名称创建了一个精灵,在spriteArray多次引用了它。 Create a new one each time: 每次创建一个新的:

private void createNewSprite() {
    Sprite sprite = new Sprite(spriteMap.get(name)); //creates a new sprite that is a copy of the one in your map.
    sprite.setPosition(x, y);
    sprite.rotate(rotation);
    spriteArray.add(sprite);
}

They are only slightly heavy. 他们只是有点沉重。 Each Sprite instance has an array of 20 floats for the vertex data, which contains redundant UV data for the underlying TextureRegion, and a redundant Color object and Rectangle object. 每个Sprite实例都有一个包含20个浮点的数组,用于顶点数据,其中包含基础TextureRegion的冗余UV数据,以及冗余的Color对象和Rectangle对象。 So it is heavier than creating your own class that references all these things without the redundancies. 因此,这比创建自己的类来引用所有这些内容而没有多余内容要重。

They are too heavy to be creating and destroying many of them during game play without using pooling. 它们太重了,无法在不使用池的情况下在游戏过程中创建和销毁它们中的许多。

What would really be heavy is loading new Textures to create your Sprites instead of them all sharing a single Texture instance. 真正麻烦的是加载新的Textures来创建Sprites,而不是让它们全部共享一个Texture实例。

In my opinion Libgdx Sprites are generally not good for game design because they conflate state with drawing too much, aside from the redundancies. 在我看来,Libgdx Sprites通常不适用于游戏设计,因为除了冗余之外,它们将状态与绘制过多混合在一起。 They are useful for particles (and maybe UI elements) where the conflation is not really a design issue. 它们对于合并实际上不是设计问题的粒子(可能还有UI元素)很有用。 In fact, I think these are the only two places Sprites are used internally in Libgdx. 实际上,我认为这是Libgdx内部仅使用Sprites的两个地方。

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

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