Libgdx - How to draw filled rectangle in the right place in scene2d?

前端 未结 5 2038
忘了有多久
忘了有多久 2021-01-02 02:23

I am using scene2d. Here is my code:

group.addActor(new Actor() {

    @Override
    public Actor hit(float arg0, flo         


        
5条回答
  •  盖世英雄少女心
    2021-01-02 02:48

    The best solution for me. Cause when you using ShapeRenderer it's doesn't react on moving/zooming camera.

    public class Rectangle extends Actor {
    
        private Texture texture;
    
        public Rectangle(float x, float y, float width, float height, Color color) {
            createTexture((int)width, (int)height, color);
            setX(x);
            setY(y);
            setWidth(width);
            setHeight(height);
        }
    
        private void createTexture(int width, int height, Color color) {
            Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
            pixmap.setColor(color);
            pixmap.fillRectangle(0, 0, width, height);
            texture = new Texture(pixmap);
            pixmap.dispose();
        }
        
        @Override
        public void draw(Batch batch, float parentAlpha) {
            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            batch.draw(texture, getX(), getY(), getWidth(), getHeight());
        }
    }
    

提交回复
热议问题