Topic was automatically imported from the old Question2Answer platform.
Asked By
ILearnToCode
This is my code
extends Node2D;
var change;
var change1;
func _process(delta):
change=0;
if Input.is_key_pressed(KEY_RIGHT):
change= 1
elif Input.is_key_pressed(KEY_LEFT):
change= -1
elif Input.is_key_pressed(KEY_UP):
change1= 1
elif Input.is_key_pressed(KEY_DOWN):
change1= -1 #sprite.global_position
$Camera2D.position = Vector2(get_position_in_parent()+change);
pass;
If you know why I get an error please tell me, the error is, “Invalid call. Nonexistent ‘Vector2’ constructor.”
Nonexistent constructor means that you haven’t passed the correct arguments to the constructor, “Vector2()”. “Vector2()” can take different types of arguments, but not what you’re giving it. I suspect that “getpositionin_parent()” is returning a number. Whatever it’s returning isn’t useful for making a vector.
Your variable change, after the if-else block, has been assigned to be an integer. You are trying to create a vector2D by summing a vector (get_position()) and an integer. Vector2D + integer = undefined. So the interpreter can’t find a constructor (a way to create) a vector2.
If you take a look at the docs (through godot editor>Help>Search>Vector2), there’s a constructor for Vector2 in the form: Vector2 Vector2(x: float, y:float)
and at the Properties section, x and y is default to be 0.
To conclude my answer, what you want to do would be: