Error when entering flip_h as True or False

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

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 _physics_process(_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

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

whiteshampoo | 2020-05-29 06:57

:bust_in_silhouette: Reply From: deaton64

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  
:bust_in_silhouette: Reply From: Ceremolligence

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!