|
|
|
![:information_source: :information_source:](https://forum.godotengine.org/images/emoji/twitter/information_source.png?v=12) |
Attention |
Topic was automatically imported from the old Question2Answer platform. |
![:bust_in_silhouette: :bust_in_silhouette:](https://forum.godotengine.org/images/emoji/twitter/bust_in_silhouette.png?v=12) |
Asked By |
Unireket Games |
extends Area2D
Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
var speed = 400
var screen_size
screen_size = get_viewport_rect().size
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
pass
var velocity = Vector2.ZERO
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()
Remove the pass in your code
Venex2004 | 2023-05-22 06:26
|
|
|
![:bust_in_silhouette: :bust_in_silhouette:](https://forum.godotengine.org/images/emoji/twitter/bust_in_silhouette.png?v=12) |
Reply From: |
captdane3 |
What is your code supposed to do?
Attach the script to a CharacterBody2D node instead of an Area2D node.
You can right click the node and choose Change Type
I assume you want your character to move but your character doesn’t?
In that case, try adding
move_and_slide()
under
func _process(delta):
and you don’t need pass in your code so you can just erase it.
func _process(delta):
var velocity = Vector2.ZERO
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()
move_and_slide()
But if you intentionally want an Area2D
node to move then change its position like this:
func _process(delta):
var velocity = Vector2.ZERO
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
position += velocity * delta
$AnimatedSprite2D.play()
else:
$AnimatedSprite2D.stop()