【问题标题】:Expected an indented block after "if"在“if”之后需要一个缩进块
【发布时间】:2022-01-19 15:33:16
【问题描述】:

我做错了什么这是我的代码,我的错误在第二行我也在使用 godot,我正在使用 gdscrptgodot这不是我的所有代码它只是我得到错误的代码

if is_on_wall():
if spritedir == "left" and test_move(transform, Vector2(-1,0)):
    anim_switch("push")
if spritedir == "right" and test_move(transform, Vector2(1,0)):
    anim_switch("push")
if spritedir == "up" and test_move(transform, Vector2(0,-1)):
    anim_switch("push")
if spritedir == "down" and test_move(transform, Vector2(0,1)):
    anim_switch("push")

【问题讨论】:

    标签: godot gdscript


    【解决方案1】:

    正如错误所说,您需要在第一个 if 之后缩进代码:

    if is_on_wall():
        if spritedir == "left" and test_move(transform, Vector2(-1,0)):
            anim_switch("push")
        if spritedir == "right" and test_move(transform, Vector2(1,0)):
            anim_switch("push")
        if spritedir == "up" and test_move(transform, Vector2(0,-1)):
            anim_switch("push")
        if spritedir == "down" and test_move(transform, Vector2(0,1)):
            anim_switch("push")
    

    【讨论】:

      【解决方案2】:

      GDScript 的语法与 Python 相似。当您声明像“if”这样的条件语句时,您需要在条件下方缩进代码。

      if x == y:
          # All the code with indent.
          pass
      

      当您缩进代码时,您实际上是在说它属于您的条件语句的范围。因此,如果在“if”语句后不缩进,则会带来错误,因为在 True/False 的情况下,没有什么可以执行。

      【讨论】:

        猜你喜欢
        • 2015-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-31
        相关资源
        最近更新 更多