How do I fix my code for making the attacks appear at random positions?

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

I used this code:
extends Area2D

var rand = RandomNumberGenerator.new()

onready var attacksprite = get_node(“Attack1”)

onready var timer = $AttackTimer

func _ready():
timer.start()
rand.randomize()
add_to_group(“attack”)

set_process_input(true)
get_sprite_frames(0).get_frame_count("Attackanimation")

func _process(_delta: float) → void:
var frames = preload(“res://assets/Attackanimation.tres”)
get_child(0).set_frames(1)

func _on_AttackTimer_timeout():
queue_free()

for making my attacks appear, but it’s not working. Can you please help me?

:bust_in_silhouette: Reply From: exuin

Next time you ask a question, it would be helpful to post what specific errors you’re getting.

So I see a bunch of errors here:

get_sprite_frames(0).get_frame_count("Attackanimation")

Area2D does not have the method get_sprite_frames() - it’s for AnimatedSprite and takes no arguments since it’s an accessor method. If attacksprite is the AnimatedSprite you need

attacksprite.frames.get_frame_count("Attackanimation")

Also, if attacksprite is the first child of the Area2D, there is no need to have get_child(0) when you already have a variable that points to that node. Also, set_frames() isn’t a method. You’re probably looking for set_sprite_frames(), but you can just set the frames property directly since there are no private properties in GDScript. It doesn’t take an int as an argument but rather a SpriteFrames resource, which I’m guessing is the frames variable.

I suggest that you read some more tutorials on GDScript so you understand how methods and variables work.