Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Unireket Games |
extends Area2D
Called when the node enters the scene tree for the first time.
func _ready():
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
var velocity = Vector2.ZERO
var speed = 400
var screen_size
screen_size = get_viewport_rect().size
if Input.is_action_pressed("right"):
velocity.x += 1
if Input.is_action_pressed("left"):
velocity.x -= 1
if Input.is_action_pressed("up"):
velocity.y -= 1
if Input.is_action_pressed("down"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite2D.play()
else:
$AnimatedSprite2D.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)