loading regions of textureatlas in libgdx loads the entire file

时光毁灭记忆、已成空白 提交于 2019-12-02 00:57:10

问题


I managed to pack a textureatlas with images i have and it works properly in that it creates the .pack file and the .png file. The issue is when i load the texture atlas and try to assign AtlasRegions. it loads the entire atlas instead of only the image i want. here is a mockup code i used to test it.

@Override
public void create() {      
    camera = new OrthographicCamera(800, 480);
    batch = new SpriteBatch();

    test = new TextureAtlas(Gdx.files.internal("polytest.png"));

    sprite = test.findRegion("hero");
    texture = new Texture(Gdx.files.internal("data/libgdx.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);


}

@Override
public void render() {      
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(sprite.getTexture(), 10, 10);
    batch.end();
}

this renders the entire atlas instead of only the image called "hero" in the atlas. Why does this happen?

Thanks in advance.


回答1:


When you do.-

batch.draw(sprite.getTexture(), 10, 10);

you're actually telling libgdx to render the whole Texture. What you need is to draw just the TextureRegion.-

batch.draw(sprite, 10, 10);


来源:https://stackoverflow.com/questions/19774201/loading-regions-of-textureatlas-in-libgdx-loads-the-entire-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!