0 votes

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
const SPEED = 200
const JUMP_HEIGHT = -550

var motion = Vector2()

func physicsprocess(delta):
motion.y += GRAVITY
var friction = false

    if Input.is_action_pressed("right"):
        motion.x = min(motion.x+ACCELERATION, SPEED)
        $AnimatedSprite.flip_h = false
        $AnimatedSprite.play("walk")
    elif Input.is_action_pressed("left"):
        motion.x = max(motion.x-ACCELERATION, -SPEED)
        $AnimatedSprite.flip_h = true
        $AnimatedSprite.play("walk")
    else:
            $AnimatedSprite.play("idle")
            friction = true

    if is_on_floor():
        if Input.is_action_pressed("up"):
            motion.y = JUMP_HEIGHT
        if friction == true:
            motion.x = lerp(motion.x,0 ,0.2)
    else:
        if motion.y <0:
            $AnimatedSprite.play("jump")
        else:
            $AnimatedSprite.play("fall")
        if friction == true:
            motion.x = lerp(motion.x,0 ,0.5)
    motion = move_and_slide(motion, UP)

whats the prob of my code?
it says that attempt to call function on null instance
on line 24($AnimatedSprite.play("idle"))

PS: it works fine with i didnt insert the camera 2d
but when i insert the camera2d it just promt a message : attempt to call function on null instance

please help im new in this !

in Engine by (17 points)

"Null instance" means that Godot couldn't find the node since it doesn't exist. "$AnimatedSprite" looks for the node called "AnimatedSprite" that is the direct child of the KinematicBody2D. Do you have a node like that?

yes i have a node of that, the error happen when i inserted the camera2d as a child in the animationsprite, it works fine when i didnt insert camera2d in the animetedsprite

Where did you put the Camera2D? Was it indented more than the AnimatedSprite (as a child)?

i put camera2d on more than animatedsprite, is there a conflict for putting camera2d on multiple sprite?

No, can you post your node layout?

enter image description here

enter image description here

see the link

1 Answer

0 votes
Best answer

Your problem is that you don't have a node named "AnimatedSprite". It's named "patrick". You either need to change the paths in your code or change the name of the node to match.

by (7,452 points)
selected by

I see, i get it now bro thanks

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.