Search code examples
game-developmentgodotgodot4

How to fix error "Invalid get index 'flip'(on base: 'AnimatedSprite2D')"


Hey I am trying to make a 2D platformer with sprite animations, but I keep getting this error "Invalid get index 'flip'(on base: 'AnimatedSprite2D')". This is my code, does anyone have a idea of what im doing wrong? I was expecting the player to move to the left without the game chrasing.

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim = get_node("AnimationPlayer")

func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta

if Input.is_action_just_pressed("ui_accept") and is_on_floor():
    velocity.y = JUMP_VELOCITY
    anim.play("Jump")

var direction = Input.get_axis("ui_left", "ui_right")
print(direction)
if direction == -1:
    get_node("AnimatedSprite2D").flip.h = true
elif direction == 1:
    get_node("AnimatedSprite2D").flip.h = false
    
if direction:
    velocity.x = direction * SPEED
    if velocity.y == 0:
        anim.play("Run")
    
    velocity.x = move_toward(velocity.x, 0, SPEED)
    if velocity.y == 0:
        anim.play("Idle")
        
    if velocity.y > 0:
        anim.play("Fall")

move_and_slide()

Solution

  • Per the AnimatedSprite2D docs, the property to use is called flip_h, not flip.h.

    Try replacing your code with:

    if direction == -1:
        get_node("AnimatedSprite2D").flip_h = true
    elif direction == 1:
        get_node("AnimatedSprite2D").flip_h = false