Move a sprite

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

I made a script to move out of point A and go to point B and then back to point A and do so forever.
But it only makes the first block of the script and stops.

extends Node2D

var speed = 200
var pos_inicial
var pos_final 

func _ready():
    set_process(true)
    pos_inicial = true
    pos_final = false

func _process(delta):
    var move = Vector2()
    if $".".position.x <= 700 && pos_inicial == true:
	    move.x = speed
	    if $".".position.x >= 700:
		    pos_inicial = false
		    pos_final = true
		
    if $".".position.x >= 10 && pos_final == true:
	    move.x = -speed
	    if $".".position.x <= 10:
		    pos_inicial = true
		    pos_final = false
		
    position += move * delta
:bust_in_silhouette: Reply From: jandrewlong

You are checking if the sprite should move the other direction before moving it past the limit.

You do not need the position checks on the outer if blocks, the pos_inicial and pos_final.

This should work:

var speed = 200
var pos_inicial
var pos_final 

func _ready():
    set_process(true)
    pos_inicial = true
    pos_final = false

func _process(delta):
    var move = Vector2()
    if pos_inicial:
        move.x = speed
        if position.x >= 700:
            pos_inicial = false
            pos_final = true

    if pos_final:
        move.x = -speed
        if position.x <= 10:
            pos_inicial = true
            pos_final = false

    position += move * delta