0 votes
func _input(event):
    if anim == "idle" and event.is_action_pressed("move_left"):
        anim = "lilstep"
        direction = -1

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.

in Engine by (225 points)

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

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)


1 Answer

0 votes

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: http://docs.godotengine.org/en/2.1/classes/class_kinematicbody2d.html

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.

by (21,920 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.