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

前端 未结 5 2036
忘了有多久
忘了有多久 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:56

    You need to convert the Actor's local coordinates into screen coordinates. Assuming your stage is full-screen, you can just use Actor.localToStageCoordinates:

    vec.set(getX(), getY());
    this.localToStageCoordinates(/* in/out */ vec);
    shapeRenderer.filledRect(vec.x, vec.y, getWidth(), getHeight());
    

    Where vec is a private Vector2d (you don't want to allocate a new one on each render call).

    This is also assuming that your ShapeRenderer is defined to be map to the full screen (which is the default).

    Also, if you switch away from the ShapeRenderer and back to the SpriteBatch, note that the batch is already adjusted to Actor coordinates (and thus you can use getX() and getY() directly with batch.draw(...).

提交回复
热议问题