Unable to access set_pos for Node2D

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

Hi,

Sorry for silly question (this is my first 4 hrs with Godot).

I’m unable to access set_pos method from Node2D object

Error message:
Invalid call. Nonexistent function ‘set_pos’ in base ‘CanvasLayer’.

Snippet

extends KinematicBody2D

var motion = Vector2(0, 0)
const SPEED = 1000
const GRAVITY = 300
const UP = Vector2(0, -1)
const JUMP_SPEED = 4000
const WORLD_LIMIT = 8000

signal animate

func _ready():
	pass

func _physics_process(delta):
	apply_gravity()
	jump()
	move()
	animate()
	move_and_slide(motion, UP)
	touch_controls()

func touch_controls():
	#	get_node("Camera2D/LeftCanvas").position = $Camera2D.get_camera_position()
#	$Camera2D.get_camera_position()
	var viewport = get_node("/root").get_children()[0].get_viewport_rect().size
	var viewport_res = get_node("/root").get_children()[0].get_viewport_rect().size
	var viewport_scale = 600/viewport.y
	
	$Camera2D/LeftCanvas/LeftControls.set_pos(Vector2(0, 500))
	print(viewport_res)
	

func animate():
	emit_signal("animate", motion)

func apply_gravity():
	if position.y > WORLD_LIMIT:
		end_game()
	if is_on_floor():
		motion.y = 0
	elif is_on_ceiling():
		motion.y = 2
	else:
		motion.y += GRAVITY

func end_game():
	get_tree().change_scene("res://Scenes/GameOver.tscn")
	
func jump():
	if Input.is_action_pressed("Jump") and is_on_floor():
		motion.y -= JUMP_SPEED
	
func move():
	if Input.is_action_pressed("Right") and not Input.is_action_pressed("Left"):
		motion.x = SPEED
	elif Input.is_action_pressed("Left") and not Input.is_action_pressed("Right"):
		motion.x = -SPEED
	else:
		motion.x = 0
:bust_in_silhouette: Reply From: kidscancode

You have a couple of problems here:

  • There is no method called set_pos() in Godot - not since Godot 3.0 was released. Names were changed: pos to position, rot to rotation, etc. I suspect you may be looking a very old tutorial.

  • You’re trying to set a position on a CanvasLayer. CanvasLayer objects don’t have a position. CanvasLayer — Godot Engine (latest) documentation in English

  • You’re not using move_and_slide() correctly. In order for is_on_floor() to work properly, you must apply constant gravity, not remove it when on the ground. move_and_slide() returns a modified velocity vector for just this reason.

Since you’re new to Godot, I highly recommend you look at the official docs. The best place to start is here:

And there is information about using kinematic bodies here: