libgdx - Group.draw中的ShapeRenderer渲染颜色错误

Hoà*_*ơng 2 java colors libgdx

MyGroup:

public class GShape extends Group{
private ShapeRenderer shape;

public GShape() {
    super();
    shape = new ShapeRenderer();
}

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    shape.begin(ShapeType.Line);
    Gdx.gl10.glLineWidth(5);
    shape.setColor(1, 1f, 1f, 1f);
    shape.line(0, 0, 200, 100);
    shape.end();
}
}
Run Code Online (Sandbox Code Playgroud)

主要:

public class GameControl implements ApplicationListener {
private Stage stage;
private GShape gShape;

@Override
public void create() {
    stage = new Stage(480,320,false);

    Texture t = new Texture(Gdx.files.internal("data/the200.png"));
    Image i = new Image(t);
    stage.addActor(i);
    gShape = new GShape();
    stage.addActor(gShape);
}

@Override
public void dispose() {
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.draw();
//  gShape.render();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
} 
Run Code Online (Sandbox Code Playgroud)

形状颜色不是白色?为什么?

http://nw7.upanh.com/b3.s38.d3/352dd792eb77ce6df204a7af47ae1ac6_55348087.cos.jpg?rand=0.19125773780979216

P.T*_*.T. 11

你可能会得到不一致的结果,因为你是混合SpriteBatchShapeRenderer上下文.这两个都期望它们在OpenGL中"存储"以维持begin()end()调用.

Actor draw()方法在SpriteBatch begin()已经被调用的上下文中调用,因此您需要在开始之前结束它ShapeRenderer.(而且你需要SpriteBatch在返回之前重启.

像这样:

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
  super.draw(batch, parentAlpha);
  batch.end();   //  ** End the batch context
  shape.begin(ShapeType.Line);
  // .. draw lines ...
  shape.end()
  batch.begin(); // ** Restart SpriteBatch context that caller assumes is active
}
Run Code Online (Sandbox Code Playgroud)

  • 只是旁注,如果你每帧调用begin()和end()很多次(100s),你会发现许多Android设备上的性能都很大. (2认同)