0 votes

Hi im new in godot and im still a beginner,can someone help me im stuck when trying to make the sprite flip when click left and right here the command extends KinematicBody2D

var score : int = 0

var speed : int = 200
var jumpforce : int = 600
var gravity : int = 800

var vel : Vector2 = Vector2()

onready var sprite : Sprite = get_node("Sprite")

func physicsprocess(_delta):

vel.x = 0

if Input.is_action_pressed("move_left"):
    vel.x -= speed
if Input.is_action_pressed("move_right"):
    vel.x += speed

vel = move_and_slide(vel, Vector2.UP)

if vel.x < 0:
    sprite.flip_h = true
elif vel.x > 0: 
    sprite.flip_h = false   after a few second it launch i try to move and it crash....the commandline said ERROR: Node not found: Sprite.

At: scene/main/node.cpp:1381

in Engine by (12 points)

Could you please show us your Node-Tree? Are there other scripts?

2 Answers

0 votes

Hi,

The code works fine for me. Is the sprite a child of KinematicBody2D and named Sprite? Otherwise your code will not find it.

You can also reference the Sprite using a $.

extends KinematicBody2D

var score : int = 0

var speed : int = 200
var jumpforce : int = 600
var gravity : int = 800

var vel : Vector2 = Vector2()

func _physics_process(delta):

    vel.x = 0

    if Input.is_action_pressed("ui_left"):
        vel.x -= speed
        print(vel.x)
    if Input.is_action_pressed("ui_right"):
        vel.x += speed

    vel = move_and_slide(vel, Vector2.UP)

    if vel.x < 0:
        $Sprite.flip_h = true
    elif vel.x > 0: 
        $Sprite.flip_h = false  
by (1,963 points)
0 votes

Goodevening whiteshampoo, I see your struggling with that error, I've been going through the same error myself, I found different solution for you, I don't know if your watching that tutorial video called: Godot for beginners
but the guy that was making that video made it a little changing on that part, I think he changed his asset to "Sprit" instead of "Sprit Idle". Try retyping in line 11 in the brackets to ("Sprit Idle") and maybe it should work.

extends KinematicBody2D

var score : int = 0

var speed : int = 200
var jumpForce : int = 600
var gravity : int = 800

var vel : Vector2 = Vector2()

onready var sprite : Sprite = get_node("Sprit Idle")

func _physics_process(delta):

vel.x = 0

# movement inputs
if Input.is_action_pressed("move_left"):
    vel.x -= speed
if Input.is_action_pressed("move_right"):
    vel.x += speed

# applying the volocity
vel = move_and_slide(vel, Vector2.UP)

# gravity
vel.y += gravity * delta

#jump input
if Input.is_action_just_pressed("jump") and is_on_floor():
    vel.y -= jumpForce

# sprite direction
if vel.x < 0:
    sprite.flip_h = true
elif vel.x > 0:
    sprite.flip_h = false

Apologies for my lack of communication. Hope this helped, good day!

by (14 points)
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 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 webmaster@godotengine.org with your username.