繁体   English   中英

KinematicBody2D 在 Godot 中不动

[英]KinematicBody2D not moving in Godot

我为方形字符的基本移动编写了一些代码,但它似乎不起作用,没有错误,我对 if 语句的缩进进行了纠正,它认为,这是代码:

Extends KinematicBody2D
var movespeed = 500
    
func _ready():
    pass # Replace with function body.
func _physics_process(delta):
    var motion = Vector2()
    
    if Input.is_action_pressed("up"):
        motion.y -= 1
        if Input.is_action_pressed("down"):
            motion.y += 1
    if Input.is_action_pressed("left"):
        motion.x += 1
    if Input.is_action_pressed("right"):
        motion.x -= 1
    
    motion = motion.normalized()
    motion = move_and_slide(motion * movespeed)

您的代码有效,但您需要确保输入图中存在“上”“下”“左”“右”,否则,代码将运行,但角色不会移动。

为此,菜单左上角按“项目”,然后按“项目设置”,然后按 select 输入 Map 选项卡。

您需要添加操作,例如向上、向下、向左向右,然后添加它们后,在该列表的底部找到它们,并在键盘上分配您希望控制角色的键。 这就是我必须对您的代码执行的操作才能使其正常工作。

首先,您对第三个 if 语句的缩进是错误的,它应该留一个标签。

其次,如果您在"up""down"等前面添加ui_ ,假设您没有按照Stephen的建议更改设置,它应该可以工作。 默认情况下,这些映射到箭头键。

创建一个空白项目后,以下代码可以代替您现有的语句:

if Input.is_action_pressed("ui_up"):
    motion.y -= 1
if Input.is_action_pressed("ui_down"):
    motion.y += 1
if Input.is_action_pressed("ui_left"):
    motion.x += 1
if Input.is_action_pressed("ui_right"):
    motion.x -= 1

最后,开头的Extends肯定是错的,会报错,应该是extends

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM