Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Alone Developer |
extends Area2D
export (int) var speed # How fast will player move(pixels/seconds)
var screensize # Size of the game window.
func _ready():
screenseize = get_viewport_rect().size
func _process(delta):
var velocity = Vector2() # The player's movement vector.
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
I edited your post to fix the code formatting. In future, please make sure to indent any code you paste by four spaces, or use the “Code” button in the formatting bar. I also removed “light2D” from the tags, as that didn’t seem at all applicable.
kidscancode | 2018-09-07 19:39
Hello. I have the same problem with the tutorial.
Your First Game — Godot Engine (3.0) documentation in English
The trouble is with position variable.
Could you explain it ?
Thank you very much !
vapi | 2019-03-07 14:49
The person above had several problems, mostly due to copy-and-pasting the code from the web page. What is your “trouble with position variable”? What is the error? What line? If I can’t see your code there is no way to answer your question. Start a new question and paste your code, making sure to use the formatting button so that it is readable.
kidscancode | 2019-03-07 15:23