How to center ParallaxBackground inside a Control with anchors set to Center?

mikegmikeg Posts: 2Member

Hi!

In general...

When I place a Sprite inside a Control with all anchors set to Center it works fine when the game window size changes, that is the Sprite stays in the center of the screen.

How to achieve the same centering of ParallaxBackground inside a Control? It seems to stay top-left anchored...

To be concrete...

I have the following scene hierarchy:

Also I run the following function on viewport "size_changed" signal to keep game screen responsive to fit multiple resolutions as described in QA https://godotengine.org/qa/9947/responsive-to-fit-multiple-resolutions:

func on_viewport_size_changed ():
            var size = OS.get_window_size()
            scale_factor = SAFE_SIZE.y / size.y
            window_size = Vector2 (size.x * scale_factor, SAFE_SIZE.y)
            if window_size.x < SAFE_SIZE.x:
                scale_factor = SAFE_SIZE.x / window_size.x
                window_size = Vector2 (SAFE_SIZE.x, window_size.y * scale_factor)
            if window_size.y < SAFE_SIZE.y:
                scale_factor = SAFE_SIZE.y / window_size.y
                window_size = Vector2 (window_size.x * scale_factor, SAFE_SIZE.y)
            viewport.set_size_override( true, window_size )

And it seems to work fine with "ordinary" sprites, that is ship-sprite and background-sprite stays always centered inside a Control:

No matter how window size have changed:

But not with parallax background which stays always top-left anchored even if put inside a Control with anchors set to Center:

No matter how window size have changed:

Thanks!

Answers

  • 1000h1000h Posts: 79Member

    I might try getting the 2D camera's screen center with get_camera_screen_center and then setting the paralx background to the point that function returns

  • mikegmikeg Posts: 2Member

    @1000h said:
    I might try getting the 2D camera's screen center with get_camera_screen_center and then setting the paralx background to the point that function returns

    Which property of parallax background you would set exactly? Thanks.

  • 1000h1000h Posts: 79Member

    I see that to do what I was thinking you migh tneed to make a new transform and set the origin of the the transform to the center point. If that doesn't work I would go to the project settings-> display and see if setting the stretch mode differently helps you

Sign In or Register to comment.