can i move node2d?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bgegg

extends Node2D
var speed = 100
var vel = Vector2()

func _ready():
set_process(true)

func _process(delta):
if Input.is_key_pressed(KEY_RIGHT):
vel = Vector2(speed, 0)
ーーーーーーーーーーーーーーーー
this is not work.Can not move node2d?

:bust_in_silhouette: Reply From: Maksanty

Of course you can, but your code is incorrect.
You are declaring vel variable for velocity and in process function you are assigning a value to it, but you are not using it anywhere.
All Node2D’s functions and variables you can see on official documentation of Godot Engine: Node2D

I know what you want to do and I would write it this way:

extends Node2D

var speed = 100
var vel = Vector2()

func _ready():
    set_process(true)

func _process(delta):
    vel = Vector2() # set vel to Vector2(0, 0)
    if Input.is_key_pressed(KEY_RIGHT): # detecting right key
        vel = Vector2(speed, 0) # setting vel to desired direction
# Here would be code for KEY_UP, KEY_DOWN and KEY_LEFT
    self.translate(vel * delta) # changing node's position

Here you can find many useful tutorials to start with: Godot Engine Official Documentation - Step by step

thanks for comment!it work well.I will decompose it and learn it.

bgegg | 2019-03-11 20:25