0 votes

Hi,

I have some problems with my game because of position of something like background and spawn enemies.

I make a code to always show background on full screen to don't show that gray mark.

On macbook i get this:
https://imgur.com/8f5AJ3L

And on tablet i get this:
https://imgur.com/McT7hjf

And i think the same problem happen on game, because on macbook if i don't resize if place correctly, but if i resize it place it wrong.

On macbook i get this:
https://imgur.com/eL1kA51

And on tablet i get this:
https://imgur.com/gGMpAet

I think that all problems are related to wrong calc. My function to calc background position:

extends Node2D

func _ready():
    var viewportWidth = get_viewport().size.x
    var viewportHeight = get_viewport().size.y

    var scale = viewportWidth / $Background.texture.get_size().y

    $Background.set_position(Vector2(viewportWidth/2, viewportHeight/2))
    $Background.set_scale(Vector2(scale, scale))

    print("[MainMenu] Viewport size: ", viewportWidth, "/", viewportHeight)

func _on_ButtonStart_pressed():
    get_tree().change_scene("res://Scenes/Stage1.tscn")

AND now my code in game to position my enemy spawn:

func spawn():
    var spawn = enemy1.instance()
    var vp_size = get_viewport().size
    var spawn_size = spawn.get_node("Sprite").frames.get_frame("normal", 0).get_size()
    var pos_x = (spawn_size.y / 2) + vp_size.x
    var pos_y = rand_range((spawn_size.y / 2), (vp_size.y - (spawn_size.y / 2)))
    spawn.position = Vector2(pos_x, pos_y)
    add_child(spawn)

    print("[Game] Spawn at: ", pos_x, "/", pos_y)

Can anyone help me understand it?

Im using latest version of godot.

asked Mar 4 in Engine by PRSoluções (18 points)

I have published my game with this problem. If anyone can help me will be nice.

Download here for iOS and Android:
https://flyforcoins.prsolucoes.com/

Thanks.

1 Answer

+1 vote

I would try using the 2d stretch mode and expand stretch aspect as explained in the Multiple resolutions documentation. Once these options are selected, you don't have to do anything specific to support multiple resolutions anymore.

answered Mar 5 by Calinou (5,674 points)

Hi,

Thanks, but i already use it. I tried fixed height and now expand.

See the screenshot on iphone, ipad and android:
https://imgur.com/8rC1Dy5

Obs: The black part is the notch for camera, so ignore it.

And now on macos:
https://imgur.com/3PAum0n

The strange is that players and items are correctly positioned.

My code of background position:

extends Node2D

func _ready():
    var viewportWidth = get_node("/root/World").get_viewport_rect().size.x
    var viewportHeight = get_node("/root/World").get_viewport_rect().size.y

    var scale = viewportWidth / $Background.texture.get_size().y

    $Background.set_position(Vector2(viewportWidth/2, viewportHeight/2))
    $Background.set_scale(Vector2(scale, scale))

    print("[MainMenu] Viewport size: ", viewportWidth, "/", viewportHeight)

Did you understand what can be wrong?

Thanks.

Now the problem can be understand better.

There is a margin on left size that put all to right, but the controls still centralized.

See here:

enter image description here

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.