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

前端 未结 5 2024
忘了有多久
忘了有多久 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 03:01

    As Rod Hyde mentions, ShapeRenderer has its own transform matrix and projection matrix. So you would have to get the SpriteBatch's projection Matrix first. I am not sure if there is an elegant way to do it, I did it like this:

    public class myActor extends Actor{
    
        private ShapeRenderer shapeRenderer;
        static private boolean projectionMatrixSet;
    
        public myActor(){
            shapeRenderer = new ShapeRenderer();
            projectionMatrixSet = false;
        }
    
        @Override
        public void draw(SpriteBatch batch, float alpha){
            batch.end();
            if(!projectionMatrixSet){
                shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
            }
            shapeRenderer.begin(ShapeType.Filled);
            shapeRenderer.setColor(Color.RED);
            shapeRenderer.rect(0, 0, 50, 50);
            shapeRenderer.end();
            batch.begin();
        }
    }
    

提交回复
热议问题