in Godot 4 this method is not possible because they removed import flags
kaifungamdev | 2023-05-11 21:00
You can do it in 4 if you use a TextureRect, set stretch mode to Tile and Texture → Repeat to Enabled. I added another scene to GitHub - rakkarage/TestBackground which shows this too.
It is because when you detect that it needs to be reset it has already passed the end by some amount which needs to be added to the reset value to close the gap.
extends Node2D
var off_screen
var speed = -500
var size
# Called when the node enters the scene tree for the first time.
func _ready():
off_screen = $BackgroundEmpty.position.x * -1
size = $BackgroundEmpty.texture.get_size() * $BackgroundEmpty.scale
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
$BackgroundEmpty.position.x += speed * delta
$BackgroundEmpty2.position.x += speed * delta
if $BackgroundEmpty.position.x <= off_screen:
$BackgroundEmpty.position.x = $BackgroundEmpty2.position.x + size.x
if $BackgroundEmpty2.position.x <= off_screen:
$BackgroundEmpty2.position.x = $BackgroundEmpty.position.x + size.x