Welcome to the forums @ginestopo!
Line 15 isn't defining it again, it's just setting it to an empty Vector2. You could also write the code as:
velocity = Vector2.ZERO
# or
velocity.x = 0;
velocity.y = 0
# or
velocity += 0
I'm guessing the reason the code is setting it to an empty Vector2 is to make the values of the Vector2 zero to clear the previous inputs. Without this line of code, if you pushed a direction and then let go of the key, it would keep moving in the last pressed direction rather than stopping once the direction is no longer pressed.
Side note:
If you were defining the variable again, the code would look like var velocity = Vector2()
. This would "work" in the sense that the code wouldn't crash, but the function variable (var velocity
) would shadow the velocity class variable in line 5, making it inaccessible for the function that defines the function variable. These types of bugs can be difficult to pin down, so most times unique variable names are highly encouraged to minimize accidental variable shadowing.
Also:
This discussion was caught in the moderation queue since you have not confirmed your account yet.
Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.
(Note: You do not need to repost, the discussion has been moved out and is visible now)
If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile: