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

asked May 29 in Engine by AkiKun0001 (12 points)

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

1 Answer

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  
answered May 29 by deaton64 (1,263 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.