Game work on PC but not on Tablet because of wrong position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PRSoluções

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:
Imgur

And on tablet i get this:
Imgur

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:
Imgur

And on tablet i get this:
Imgur

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.

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.

PRSoluções | 2020-03-09 04:45

:bust_in_silhouette: Reply From: Calinou

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.

Hi,

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

See the screenshot on iphone, ipad and android:
Imgur: The magic of the Internet

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

And now on macos:
Imgur: The magic of the Internet

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.

PRSoluções | 2020-03-06 04:11

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

PRSoluções | 2020-03-06 04:42

Hello , I also faced the exact same problem with my game…it works fine on mobile devices. But when i play it on a tablet i also get the same problem. I’m guessing you are using camera 2D nod?.. Remove the camera nod and try, it should be centered as you expected. I think it could be a Bug of camera nod. But still i also have no solution to the problem, as you did i also might publish the game like this. But if there is a way of move screen without the camera nod. I think you can find a solution. Also i have different scenes with bg images, all of them are centered correctly. only one i cannot centered is the one with camera 2d nod. If you found any solution please let me know. Thank You

MoonLightKnight | 2021-11-10 16:59