Godot学习笔记(3)—— GDScript(2)

1. 处理玩家输入

  1.1 使用 Input

  Input使用简单,在处理输入信号的时候优先级高,适合处理玩家移动等动作。

  Input函数每帧检查一次。

  1.2 使用_unhandled_input()

  _unhandled_input()将处理所有没有被处理过的输入信号,如果没有匹配的项目,信号将被丢弃。

  因此使用 _unhandled_input() 时要注意,信号可能被其他函数拦截,如 gui_input.

2. 处理转向

  2.1 增加变量 direction 用于处理转向。

func _process(delta):
    var direction = 0
    if Input.is_action_pressed("ui_left"):
        direction = -1
    if Input.is_action_pressed("ui_right"):
        direction = 1

    rotation += angular_speed * direction * delta

 

  X轴的左侧为负,右侧为正。

  当按下“左”时,方向为负;当按下“右”时,方向为正。

  图标的运动角度受变量影响,按下“左”时逆时针旋转,按下“右”时顺时针旋转。

  初始时变量为0,即不旋转。

  松开按键后图标停止旋转(Input.is_action_pressed 为 false)。

  2.2 增加函数处理前进。

var velocity = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
    velocity = Vector2.UP.rotated(rotation) * speed

position += velocity * delta

  Vector2.ZERO 代表长度为0的二维向量。

  

posted @ 2022-05-02 14:55  羅生門  阅读(597)  评论(0)    收藏  举报