I'm not an expert but I think it'll work when you'll put the motion var to func _ready(). And also you've wrote-
var motion = Vector2()
you must try:
var motion = Vector2(0, 0)
Try that, it must work. If dosen't worked yet tell me what is the error is telling.