本章介绍2D图形相关的一些基本内容,包括如何使用Texture,TextureRegion和SpriteBatch。
public class TextureFun implements ApplicationListener { private Texture druidTexture; // #1 private SpriteBatch batch; // #2 @Override public void create() { druidTexture = new Texture(Gdx.files.internal("druid.png")); // #3 batch = new SpriteBatch(); // #4 } @Override public void render() { batch.begin(); // #5 batch.draw(druidTexture, 100, 100); // #6 batch.end(); // #7 } // … rest of methods omitted … // }1. 声明一张纹理
... batch.enableBlending(); batch.setBlendFunction(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); ...
public void render() { batch.begin(); batch.draw(druidTexture, 100, 100); batch.draw(druidTexture, 200, 100, 32, 32, 64, 64, 1f, 2.0f, 45f, 0, 0, 64, 64, false, false); batch.end(); }
public class TextureFun implements ApplicationListener { private Texture texture; // #1 private SpriteBatch batch; private TextureRegion[] regions = new TextureRegion[4]; // #2 @Override public void create() { texture = new Texture(Gdx.files.internal("sprite_sheet.png")); batch = new SpriteBatch(); regions[0] = new TextureRegion(texture, 0, 0, 64, 64); // #3 regions[1] = new TextureRegion(texture, 0.5f, 0f, 1f, 0.5f); // #4 regions[2] = new TextureRegion(texture, 0, 63, 64, 64); // #5 regions[3] = new TextureRegion(texture, 0.5f, 0.5f, 1f, 1f); // #6 } @Override public void render() { batch.begin(); batch.draw(texture, 0, 0, 64, 64); // #7 for (int i = 0; i < regions.length; i++) { batch.draw(regions[i], 75 * (i + 1), 100); // #8 } batch.end(); } // … rest of methods ommited... // }
TextureRegion[][] regions = TextureRegion.split(texture, 64, 64)