0 votes
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

    position += velocity * delta
    position.x = clamp(position.x, 0, screensize.x)
    position.y = clamp(position.y, 0, screensize.y)
in Engine by (25 points)
edited by

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.

Hello. I have the same problem with the tutorial.
The trouble is with position variable.
Could you explain it ?
Thank you very much !

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.

1 Answer

+1 vote
Best answer

I'm guessing from the way your copy-and-pasted code was misformatted, that your indentation on those last three lines is incorrect. They belong to the _process() function, and so they should be indented one more level (lined up with the other lines, such as the if statements).

Also, note that you've spelled screensize wrong in _ready()

by (21,920 points)
selected by

yeah, following the tutorial is a bit wonky

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.