Libgdx-如何在scene2d中的正确位置绘制填充的矩形? [英] Libgdx - How to draw filled rectangle in the right place in scene2d?

查看:130
本文介绍了Libgdx-如何在scene2d中的正确位置绘制填充的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scene2d.这是我的代码:

I am using scene2d. Here is my code:

    group.addActor(new Actor() {
        @Override
        public Actor hit(float arg0, float arg1) {return null;}
        @Override
        public void draw(SpriteBatch batch, float arg1) {
            batch.end();
            shapeRenderer.begin(ShapeType.FilledRectangle);
            shapeRenderer.setColor(Color.RED);
            shapeRenderer.filledRect(0, 0, 300, 20);
            shapeRenderer.end();
            batch.begin();
        }
    });

问题是它相对于屏幕(x = 0,y = 0)绘制了这个矩形,但是我需要相对于我的 group 绘制它.但是如果我用以下方法绘制其他实体:

The problem is that it draws this rectangular relative to screen (x = 0, y = 0), but I need it to be drawn relative to my group. But if I draw other entities with:

batch.draw(texture, 0, 0, width, height);

它相对于我的组(组左下角的0,0像素)正确绘制了(x = 0,y = 0).

it correctly draws at (x = 0, y = 0) relative my group (0,0 pixels from left-bottom corner of the group).

任何建议如何在Scene2d中实现形状绘制?有人可以解释为什么这两个呼叫的工作方式不同吗?

Any suggestions how can I implement shape drawing in scene2d? And can someone can explain why these two calls work differently?

推荐答案

SpriteBatch 中的内容分开, scene2d Stage 使用.如果您更新ShapeRenderer的矩阵以匹配当

ShapeRenderer has its own transform matrix and projection matrix. These are separate to those in the SpriteBatch that the scene2d Stage uses. If you update the ShapeRenderer's matrices to match those that scene2d is using when Actor.draw() is called then you should get the results that you want.

这篇关于Libgdx-如何在scene2d中的正确位置绘制填充的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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