Move a sprite a single pixel

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider
:warning: Old Version Published before Godot 3 was released.
func _input(event):
	if anim == "idle" and event.is_action_pressed("move_left"):
		anim = "lilstep"
		direction = -1
		player.set_flip_h(false)
		player.play(anim)

I’m working on a MegaMan styled engine. The above handles whenever the player presses left or right while on the ground. I want to move the sprite left or right a single pixel as part of this function but I’m unsure how to accomplish this. I tried using set_pos(), but that returned an error.

What error did set_pos() return? What kind of node are you using for the player?

kidscancode | 2017-07-31 20:12

I’m using KinematicBody2D for the sprite with AnimatedSprite and CollisionShape2D as child nodes. The error that I’m getting back is: Invalid type in function ‘set_pos’ in base ‘KinematicBody2D (player.gd)’. Cannot convert argument 1 from float to Vector2.

I’m thinking that the syntax I’m using is just wrong. I was using
set_pos(pos.x - X_SPEED)
after

player.play(anim)

9BitStrider | 2017-07-31 20:16

:bust_in_silhouette: Reply From: kidscancode

I see. First, the error is because you can’t use a single number (float) with set_pos(). Instead, you must pass it a Vector2, representing both x and y coordinates. For example:

set_pos(Vector2(100, 150))

Second, if you’re using KinematicBody2D you should not be using set_pos() at all, you should be using move(). For example, to move the body one pixel to the right:

move(Vector2(1, 0))

See the documentation for KinematicBody2D here: KinematicBody2D — Godot Engine (2.1) documentation in English

The official demo also has a good example of using kinematic bodies, and there are some other tutorials I can link you if you need.

Aha. Thanks for the info. I appreciate the help.

9BitStrider | 2017-07-31 20:29