为什么我的代码不能用于Godot中的基本运动?

时间:2019-12-15 06:19:29

标签: 2d game-engine godot gdscript

我刚刚在godot中启动了一个新项目。到目前为止,我有一个png是主要角色。我已经向播放器添加了一个精灵和CollisionShape2D。当我运行游戏时,玩家不会移动(使用键)。有人知道这是怎么回事吗?这是我的代码:

df <- structure(list(House_No. = c("1a", "1a", "1a", "1a", "2a", "2a", 
"2a", "2a"), Info_On_Area = c("Names of neighbouringhouse in 100m  1b   1c    1d    1e", 
"Area of neighbouringhouse  in 100m  500  1000  1500  300", "Names of neighbouringhouse in 300m  1b   1c    1d    1e   1f    1g   1h", 
"Area of neighbouringhouse  in 300m  500  1000  1500  300  600   400  2000", 
"Names of neighbouringhouse in 100m  2b   2c    2d    2e", "Area of neighbouringhouse  in 100m  500  1000  1500  300", 
"Names of neighbouringhouse in 300m  2b   2c    2d    2e   2f    2g   2h", 
"Area of neighbouringhouse  in 300m  500  1000  1500  300  600   400  2000"
)), class = "data.frame", row.names = c(NA, -8L))

编辑:

我也尝试过直接从Godots文档中复制和粘贴代码,但播放器仍然无法移动:

extends KinematicBody2D

export var speed = 10.0
export var tileSize = 32.0

var initpos = Vector2()
var dir = Vector2()
var facing = 'down'
var counter = 0.0

var moving = false

func _ready():
    initpos = position

func _process(delta):
    if not moving:
        set_dir()
    elif dir != Vector2():
        move(delta)
    else:
        moving = false

func set_dir(): #set moving
    dir = get_dir()

    if dir.x != 0 or dir.y != 0:
        moving = true
        initpos = position

func get_dir(): #user input

    var x = 0
    var y = 0

    if dir.y == 0:
        x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
    if dir.x == 0:
        y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))

    return Vector2(x, y)

func move(delta): #move player linearly
    counter += delta + speed

    if counter >= 1.0:
        position = initpos + dir * tileSize
        counter = 0.0
        moving = false
    else:
        position = initpos + dir * tileSize * counter

1 个答案:

答案 0 :(得分:0)

我知道了。添加新脚本时,我没有将其添加到播放器中,而是添加到了场景中。