Making a sprite's position increase.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bunnybot5555
:warning: Old Version Published before Godot 3 was released.

Hi, I want to make a sprite move when you press a button and keep moving even when you let go.

:bust_in_silhouette: Reply From: ericdl
extends Sprite

export var move_speed = 50

var sprite_pos = Vector2()
var sprite_dir = Vector2()


func _ready():
	set_process(true)
	set_process_input(true)


func _input(event):
	if event.is_action_pressed("ui_left"): sprite_dir = Vector2(-move_speed, 0)
	if event.is_action_pressed("ui_right"): sprite_dir = Vector2(move_speed, 0)
	if event.is_action_pressed("ui_up"): sprite_dir = Vector2(0, -move_speed)
	if event.is_action_pressed("ui_down"): sprite_dir = Vector2(0, move_speed)


func _process(delta):
	sprite_pos = get_pos()
	set_pos(sprite_pos + sprite_dir * delta)
:bust_in_silhouette: Reply From: Zylann

I think most of the code to do this was given in your previous question:

Here is a simple script explaining how you can move a sprite:

extends Sprite


# Have a constant to hold how fast you want to move,
# for example 100 pixels per second
const SPEED = 100.0

# Have a variable to hold the movement direction of your sprite
var move_direction = Vector2()


func _ready():
	# Tell Godot to call _process() every frame
	set_process(true)


func _process(frame_time):
	
	# Detect when the player presses keys
	if Input.is_key_pressed(KEY_LEFT):
		move_direction = Vector2(-1, 0)
	elif Input.is_key_pressed(KEY_RIGHT):
		move_direction = Vector2(1, 0)
	elif Input.is_key_pressed(KEY_UP):
		move_direction = Vector2(-1, 0)
	elif Input.is_key_pressed(KEY_DOWN):
		move_direction = Vector2(1, 0)

	# Note: usually we set the direction to zero if the player doesn't presses any key,
	# but here you want to continue moving, so we leave move_direction as it is.

	# Calculate how many pixels the sprite has to move.
	# This formula takes the time between frames into account, so
	# if the game lags a bit, the sprite will still move at the same speed
	var motion = move_direction * SPEED * frame_time

	# Change the position of the sprite, so it will appear to move
	set_pos(get_pos() + motion)

If you are new to Godot or even programming, I strongly suggest you learn from the offocial documentation: Godot Docs – master branch — Godot Engine (latest) documentation in English
Or with some tutorials https://forum.godotengine.org/7981/any-good-tutorials?show=7981#q7981