1

我需要知道如何从纹理图集中添加一个精灵,以便我可以将它实现为我的主菜单的精灵。

这是我的主菜单代码:

public class MainMenu implements Screen {

    CrazyZombies game;
    Stage stage;
    TextureAtlas atlas;
    Skin skin;
    SpriteBatch batch;

    TextureRegion firstLayer;
    TextureRegion secondLayer;
    TextureRegion thirdLayer;
    TextureRegion fourthLayer;
    TextureRegion fifthLayer;
    TextureRegion sixthLayer;
    TextureRegion seventhLayer;
    TextureRegion eighthLayer;
    TextureRegion ninthLayer;
    TextureRegion tenthLayer;
    TextureRegion eleventhLayer;

    public MainMenu(CrazyZombies game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);

        batch.begin();
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        Gdx.input.setInputProcessor(stage);


    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        skin = new Skin();
    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        batch.dispose();
        skin.dispose();
        atlas.dispose();
        stage.dispose();
    }

    public void loadLayers() {
        TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/mainmenu/MainMenu.pack"));
        firstLayer = atlas.findRegion("1layer");
    }
}

所以我需要着手让 firstLayer 出现在屏幕上,而我尝试过的每一种方式都以崩溃告终。

4

1 回答 1

3

You are not doing it right. Your render method doesn't have any draw. Take a look at the SuperJumper demo in libgdx.

To answer on how to create a sprite-

Sprite mySprite;
...
...
mySprite = new Sprite(someTextureRegion);
        mySprite.setSize(SOME_WIDTH, SOME_HEIGHT);
        mySprite.setOrigin(SOME_WIDTH/2f, SOME_HEIGHT/2f);
        mySprite.setPosition(world.sprite.bounds.x, world.sprite.bounds.y);
...
...
mySprite.draw(batch);
于 2013-05-14T20:02:31.603 回答