1
\$\begingroup\$

I made a character in Godot 4 that enters from one side of the image and exits from the other side, which shows its different modes randomly each time. I have written these codes for it:

extends RigidBody2D

@export var min_speed = 150.0
@export var max_speed = 250.0


func _ready():
    $AnimatedSprite2D.playing = true
    var mob_types = $AnimatedSprite2D.frames.get_animation_names()
    $AnimatedSprite2D.animation = mob_types[randi() % mob_types.size()]


func _on_visible_on_screen_notifier_2d_screen_exited():
    queue_free()

but when I press the F5 key, it displays this error:

Invalid set index 'playing'(on base: 'AnimatedSprite2D')with value of type 'bool'.

What is the problem? How should I write the codes?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The error is telling you that AnimatedSprite2D does not have a playing property (to which you are trying assign a bool).

The AnimatedSprite2D can hold multiple animations, and you are supposed to tell it which one you want to play by calling the play method:

$AnimatedSprite2D.play(&"NameOfMyAnimation")

Or by setting the animation property (which are already doing).

Thus, I believe that removing the offending line is sufficient to make your code work.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .