无法同时绘制图块和阶段的图像 [英] Can't draw a ninepatch image and stage at the same time

查看:67
本文介绍了无法同时绘制图块和阶段的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试绘制一个9补丁图像和一个舞台时,最后一个被称为绘制的东西就会被绘制.我尝试使用正交摄影机,但未成功.我尝试过的:

Whenever I try to draw a ninepatch image and a stage the last thing that's called is being drawn. I have tried to use a orthographic camera but didn't succeed. What I have tried:

batch.setProjectionMatrix(camera.combined);
ninePatch.draw(batch, xPos, yPos, width, height);
stage.act(delta);
stage.draw();

camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.setCamera(camera)

代码

CreateQuiz类

The CreateQuiz class

public class CreateQuiz implements Screen{

    public CreateQuiz(Quiz quiz){
    this.quiz = quiz;
    }

    private Quiz quiz;
    private FallDownPanel fallDownPanel;
    private OrthographicCamera camera;

    @Override
    public void render(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    quiz.beginBatch();
    fallDownPanel.render(quiz.getSpriteBatch(), delta);
    quiz.endBatch();
    }

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

    }

    @Override
    public void show() {
    fallDownPanel = new FallDownPanel(FallDownPanel.START_LOWER_SIDE, 200, quiz.getTextureAtlas().createPatch("fallDownWindow"));
    Stage stage = new Stage(fallDownPanel.getWidth(), fallDownPanel.getHeight(), false);
    TextFieldStyle style = quiz.getGreenTextFieldStyle();
    style.font.scale(-0.30f);
    TextFieldQuiz test = new TextFieldQuiz("Hej åäö 123 !", style, 0, 2, 400, 16);
    test.setTextFieldListener(new TextFieldListener() {
        @Override
        public void keyTyped (TextField textField, char key) {
        if (key == '\n') {
            textField.getOnscreenKeyboard().show(false);
        }
        }
    });
    stage.addActor(test);
    Gdx.input.setInputProcessor(stage);
    fallDownPanel.setStage(stage);
    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    stage.setCamera(camera);
    fallDownPanel.setCamera(camera);
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}

FallDownPanel.class

The FallDownPanel.class

    public class FallDownPanel {

    //With repeat
    public FallDownPanel(int startSide, int size, NinePatch ninePatch){
    Tween.registerAccessor(FallDownPanel.class, new FallDownPanelTween());
    Tween.registerAccessor(Widget.class, new WidgetTween());
    tweenManager = new TweenManager();
    final int screenWidth = Gdx.graphics.getWidth();
    final int screenHeight = Gdx.graphics.getHeight();
    int target = 0;
    int tweenType = 0;

    if(startSide == START_LEFT_SIDE){
        setSize(size, screenHeight);
        xPos = -size;
        yPos = 0;
        target = 0;
        tweenType = FallDownPanelTween.POSITION_X;
    }

    else if(startSide == START_RIGHT_SIDE){
        setSize(size, screenHeight);
        xPos = screenWidth;
        yPos = 0;
        target = screenWidth - size;
        tweenType = FallDownPanelTween.POSITION_X;
    }

    else if(startSide == START_UPPER_SIDE){
        setSize(screenWidth, size);
        xPos = 0;
        yPos = screenHeight + size;
        target = screenHeight - size;
        tweenType = FallDownPanelTween.POSITION_Y;
    }

    else if(startSide == START_LOWER_SIDE){
        setSize(screenWidth, size);
        xPos = 0;
        yPos = -size;
        target = 0;
        tweenType = FallDownPanelTween.POSITION_Y;
    }

    Tween.to(this, tweenType, 1).target(target).start(tweenManager);
    this.tweenType = tweenType;
    this.startSide = startSide;

    this.ninePatch = ninePatch;
    }

    private TweenManager tweenManager;
    private NinePatch ninePatch;
    private Stage stage;
    private OrthographicCamera camera;

    private float xPos, yPos;
    private int width, height;
    private int tweenType;
    private int startSide;
    public static final int START_LEFT_SIDE = 0, START_RIGHT_SIDE = 1, START_UPPER_SIDE = 2, START_LOWER_SIDE = 3;

    public void render(SpriteBatch batch, float delta){
    tweenManager.update(delta);
    batch.setProjectionMatrix(camera.combined);
    ninePatch.draw(batch, xPos, yPos, width, height);
    stage.act(delta);
    stage.draw();
    }

    public void setX(float x){
    this.xPos = x;
    }

    public void setY(float y){
    this.yPos = y;
    }

    public float getX(){
    return xPos;
    }

    public float getY(){
    return yPos;
    }

    public float getWidth(){
    return width;
    }

    public float getHeight(){
    return height;
    }

    private void setSize(int w, int h){
    width = w;
    height = h;
    }

    public Stage getStage() {
    return stage;
    }

    public void setStage(Stage stage) {
    this.stage = stage;
    startWidgetTweens();
    }

    private void startWidgetTweens(){
    float size = getShortestSide();
    Array<Actor> actors = stage.getActors();
    for(int i = 0; i < actors.size; i++){
        Widget w = (Widget) actors.get(i);
        Tween.to(w, tweenType, 1).target(0).start(tweenManager);
    }
    }

    private float getShortestSide() {
    if(startSide == START_LEFT_SIDE){
        return width;
    }

    else if(startSide == START_RIGHT_SIDE){
        return width;
    }

    else if(startSide == START_UPPER_SIDE){
        return height;
    }

    else if(startSide == START_LOWER_SIDE){
        return height;
    }

    return -1;
    }

    public void setCamera(OrthographicCamera camera) {
    this.camera = camera;
    }

}

推荐答案

始终确保一次只有一个活动的Renderer. Renderer是:

Always make sure you have only one active Renderer at a time. The Renderers are:

  • Batch
  • SpriteBatch
  • ShapeRenderer
  • Box2DDebugRenderer如果我没记错的话
  • ModelBatch用于3D
  • Batch
  • SpriteBatch
  • ShapeRenderer
  • Box2DDebugRenderer if i am not wrong
  • ModelBatch for 3D

我希望我不会忘记一个.
确保始终调用end()作为活动对象,然后再调用begin()作为新对象.
还请记住,Stage有其自己的SpriteBatch,并且如果您调用stage.draw(),它会为此SpriteBatch调用begin().因此,请确保在stage.draw()之前先end()活动的Renderer.
如果需要ShapeRenderer或任何其他渲染器来绘制Actor(可能是调试原因),则必须结束stage s SpriteBatch,这是通过draw()方法获得的.确保在方法末尾再次begin().
希望这足够清楚.

I hope i did not forget one.
Make sure you always call end() for the active one, before calling begin() for a new one.
Also remember, that a Stage has its own SpriteBatch and it calls begin() for this SpriteBatch, if you call stage.draw(). So make sure you end() the active Renderer, before stage.draw().
If you need a ShapeRenderer or any other renderer to draw an Actor (maybe cause of debuging), you have to end the stages SpriteBatch, which you get with the draw() method. Make sure you begin() it again at the end of the method.
Hope this is clear enough.

这篇关于无法同时绘制图块和阶段的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆