为什么Godot的elif语句有问题?

问题描述

因此,这是带有elif语句的代码,该代码通常是elif语句。我对Godot相当陌生,这是我的一个榜样,并不断学习:

扩展了AnimatedSprite

func _process(delta):

 if Input.is_action_pressed("ui_right"):
  play("Run")
 flip_h = false
 elif  Input.is_action_pressed("ui_left"):
     play("Run")
    flip_h = true
    else:
        play("Idle")
 pass

我得到这个错误

error(7,3):错误解析表达式,放错了位置:elif

那会是什么?我不知道如何解决这个问题。

解决方法

这与您如何缩进代码有关。快速浏览一下documentation

ifelifelse应该都处于相同的缩进级别。语句中的所有内容都应再缩进一层。您的代码如下所示:

extends AnimatedSprite

func _process(delta): 
  if Input.is_action_pressed("ui_right"):
    play("Run")
    flip_h = false
  elif Input.is_action_pressed("ui_left"):
    play("Run")
    flip_h = true
  else:
    play("Idle")

此外,最后不需要pass,因为您正在呼叫play("Idle")