Error -> Invalid get index 'frames' (on base: 'AnimatedSprite2D').

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

my code :

 extends CharacterBody2D

@onready var animeted_sprite = $AnimatedSprite2D

const  GRAVITY = 1500
const GRAVITY_INCREMNET = 2500
const JUMP_DECREMENT = 100
const JUMP_FORCE = 400

@export var speed = 500

var screen_width
var half_sprite_width

var jumping = false
var current_jump_force = 0
var current_gravity = 0

func _ready():
	screen_width = get_viewport_rect().size.x
	half_sprite_width = animeted_sprite.frames.get_frame("idle",0).get_width() / 2
	current_gravity = GRAVITY
	

func _process(delta):
	if jumping:
		_increment_gravity(delta)
		position.y += current_gravity * delta
	else:
		position -= current_jump_force
		_decrement_jump(delta)
		
	if Input.is_action_pressed("ui_left"):
		position.x -= speed * delta
	elif Input.is_action_pressed("ui_right") :
		position.x += speed * delta
	elif Input.is_action_pressed("ui_accept") :
		jump()
			
	
func jump():
	if jumping:
		return
		
	current_gravity = 0
	jumping = true
	current_jump_force = JUMP_FORCE
	animeted_sprite.play("jump")
		
		
		
func _increment_gravity(delta):
	current_gravity += GRAVITY_INCREMNET * delta
	if current_gravity >= GRAVITY:
		current_gravity = GRAVITY
		
func _decrement_jump(delta):
	current_jump_force -= JUMP_DECREMENT * delta
	if current_jump_force <= 0:
		current_jump_force = 0
		jumping = false
		animeted_sprite.play("idle")

when i run the game it shows error Invalid get index ‘frames’ (on base: ‘AnimatedSprite2D’).

Recently, I have been learning these languages,

lajmonline | 2023-06-20 15:36

:bust_in_silhouette: Reply From: godot_dev_

According to the documentation of AnimatedSprite2D, the node has no member frames, which is what the error message you are getting is indicating. AnimatedSprite2D has a member called sprite_frames. Try changing

half_sprite_width = animeted_sprite.frames.get_frame("idle",0).get_width() / 2

to

half_sprite_width = animeted_sprite.sprite_frames.get_frame("idle",0).get_width() / 2

Next time you face such an error, pull up the documentation of the node to confirm what functions/members exist

still not working

imsolo | 2023-06-20 15:53

Are you getting the same error message?

godot_dev_ | 2023-06-20 16:38

Invalid call. Nonexistent function ‘get_frame’ in base ‘SpriteFrames’.

imsolo | 2023-06-20 16:50

Upon viewing the documentation of SpriteFrames, there is no function called get_frame, hence the error message.

What you appear to be trying to do is get the texture from the frame and get the width. The function get_frame_texture does fectch the Texture2D of the frame, and Texture2D objects do have a get_width function. So try changing it to

half_sprite_width = animeted_sprite.sprite_frames.get_frame_texture("idle",0).get_width() / 2

godot_dev_ | 2023-06-20 17:16