舞台吸引了一切

时间:2013-08-14 23:38:37

标签: java render libgdx stage

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

    stage.act(delta);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    if (lifeCount > 0) {
        /* 
        When stage.draw is called here, it only displays the exit button.
        The game still operates, but everything is invisible.
        If I leave the game on, the game over screen is shown,
        */
        stage.draw();
        text.setColor(1.0f, 1.0f, 1.0f, 1.0f);
        text.draw(batch, score + scoreCount, 25, 100);
        text.draw(batch, lives + lifeCount, 25, 120);
        text.draw(batch, speed + raindropSpeed, 25, 80);
        batch.draw(bucketTexture, bucket.x, bucket.y);
        for (Rectangle clearDrop : clearDrops) {
            batch.draw(clearDropTexture, clearDrop.x, clearDrop.y);
        }
        for (Rectangle healthDrop : healthDrops) {
            batch.draw(healthDropTexture, healthDrop.x, healthDrop.y);
            /* 
             If I place stage.draw here, health drops are invisible.
             This also happens if I place it in the raindrop for-loop and the 
             cleardrop for-loop
            */ 
        }
        for (Rectangle raindrop : raindrops) {
            batch.draw(raindropTexture, raindrop.x, raindrop.y);
        }
        /*
         If I place stage.draw here, the bucket, score, life, and speed
         display correctly. The drops are still invisible.
         */
    } else {
        pause();
        raindrops.clear();  
        game.setScreen(new GameOver(game));
    }
    batch.end();

我一直试图做的是在GameScreen的右上角有一个退出按钮,虽然绘制按钮所在的舞台会给我带来困难(请参阅代码中的注释)。

这是退出按钮和舞台(resize())的代码:

if (stage == null)
        stage = new Stage(width, height, true);
    stage.clear();

    Gdx.input.setInputProcessor(stage);

    TextButtonStyle styleQuit = new TextButtonStyle();
    styleQuit.up = skin.getDrawable("buttonnormal");
    styleQuit.down = skin.getDrawable("buttonpressed");
    styleQuit.font = text;

    quitButton = new TextButton(" ", styleQuit);
    quitButton.setWidth(128);
    quitButton.setHeight(128);
    quitButton.setX(800 - 128);
    quitButton.setY(480 - 100);

    quitButton.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
            Gdx.app.log(RainCatcher.LOG, "Quit Button Pressed");
            game.setScreen(new MainMenu(game));
        }
    });

    stage.addActor(quitButton);

其余的(在show()中)

    atlas = new TextureAtlas("gamebuttons.pack");
    skin = new Skin();
    skin.addRegions(atlas);
    text = new BitmapFont();

是否有任何特殊技巧可以让舞台与落下的雨滴,水桶和文字一起呈现?我的朋友和我都很难过,无法在任何地方找到解决方案。

2 个答案:

答案 0 :(得分:0)

在batch.end()之后或batch.begin()

之前移动stage.draw()

答案 1 :(得分:0)

这不是你认为正确的做法。如果你在libgdx中使用一个阶段,那么你应该从Scene2d的其他组件中受益。因此,不是单独绘制你的雨滴和其他游戏实体(并使你的工作变得复杂),你应该让他们成为演员,并将他们添加到你需要的舞台,然后在渲染中绘制舞台。

例如:

   public class RaindDrop extends Actor {
    TextureRegion region;

    public RaindDrop () {
            region = new TextureRegion(...);
    }

    public void draw (SpriteBatch batch, float parentAlpha) {
            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            batch.draw(...);
    }
  }

等等其他实体。初始化并将它们添加到舞台上。

这是官方维基阅读更多内容:

https://code.google.com/p/libgdx/wiki/scene2d