how do I instantiate and make a infinite scroll bg & have it automatically deleted as it goes out of view?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ANiK3T

Hi, I’m new to godot engine and was trying to make a flappy bird clone for learning.
I plan to instantiate the background sprite from right going to left such that the speed at which sprite moves and the interval between instancing its copies make the background seem infinite. As of collecting the out of view sprites i came across scripting docs and found

func _on_Visibility_screen_exited():

and

queue_free()

tried to test it but it didn’t work, (the node tab for sprite only shows Visibility_Changed() function only)

so my queries are:

  1. how to instantiate sprite backgrounds?
  2. how to manage their interval of instantiating such that they seem continous. (I’ve kept the sprite speed to 1)?
  3. how do i code the sprite’s script such that it deletes itself when exiting the view?

I’m using Godot v3, the root node is a sprite and scene contains no other node.
so far i’ve only manged to make the sprite move by the code:

extends Sprite

var vel = Vector2(1, 0)

func _ready():
    set_process(true)

func _process(delta):
    set_position(get_position() + vel)
:bust_in_silhouette: Reply From: Footurist
exends Node2D

func _ready():
    # creating 2 new sprites via script
    for i in range(2):
        var sprite = Sprite.new()
        add_child(sprite)
        sprite.name = "Sprite" + str(i)

func _process(delta):
    # move both sprites and reset their positions if they go off the
        viewport
    # assuming portrait mode at 1080 x 1920 and scrolling to the left
    for c in get_children():
        c.translate(Vector2(-360, 0) * delta) # moving 1/3 of the
            screen width per sec
        if c.position.x < -1080:
            c.position.x = 1080

Deleting them with queue_free at the end of their movement is unnecessary, since you can just string them together.