Documentation 不存在的函数在基节点2d中应用_脉冲

Documentation 不存在的函数在基节点2d中应用_脉冲,documentation,game-development,godot,gdscript,Documentation,Game Development,Godot,Gdscript,为什么当我运行它到我的应用程序时,它已经被关闭并写入-在基本节点2d中不存在函数apply_pulse。我看到Godot的文档,我找到了它的方法,你的父母是RigidByD2D,但我在视频中做了这个代码,他使用了KinematicBody2D,这很好 extends KinematicBody2D var movespeed = 500 var bulletspeed = 2000 var bullet = preload("res://Bullet.tscn") f

为什么当我运行它到我的应用程序时,它已经被关闭并写入-在基本节点2d中不存在函数apply_pulse。我看到Godot的文档,我找到了它的方法,你的父母是RigidByD2D,但我在视频中做了这个代码,他使用了KinematicBody2D,这很好

extends KinematicBody2D

var movespeed = 500 
var bulletspeed = 2000
var bullet = preload("res://Bullet.tscn") 

func _ready():
    pass 

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('right'):
        motion.x += 1
    if Input.is_action_pressed('left'):
        motion.x -= 1
    
    motion = motion.normalized() 
    motion = move_and_slide(motion * movespeed) 
    
    look_at(get_global_mouse_position())

    if Input.is_action_just_pressed('LMB'):
        fire()

func fire():
    var bullet_instance = bullet.instance()
    bullet_instance.position = get_global_position()
    bullet_instance.rotation_degrees = rotation_degrees
    bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
    get_tree().get_root().call_deferred("add_child", bullet_instance)

正如您所发现的,
apply\u pulse
存在于
RigidBody2D
(或
RigidBody2D
)中

您提供的代码使用
应用脉冲
,如下所示:

bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))

如果这样做有效,它意味着
bullet\u实例
必须是
刚体
(或
刚体2d
)。让我们看看您在哪里定义
bullet\u实例

var bullet_instance = bullet.instance()
这意味着
bullet\u实例
是您称为
bullet
PackedScene的实例。嗯,那个场景一定是一个
刚体
(或者
刚体2d
)。让我们看看您在哪里定义
项目符号

var bullet = preload("res://Bullet.tscn") 
因此,
”res://Bullet.tscn“
必须是
刚体
(或
刚体2d
)。更准确地说,该场景的根节点必须是
刚体
(或
刚体2d


另一方面,如果该线路(或其他类似线路)不工作:

bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
您得到的错误是“在基本节点2d中不存在函数'apply_pulse',那么这意味着您在
节点2d
上调用
apply_pulse
,而不是
刚体
(或
刚体

在这种情况下,这意味着
bullet\u实例
Node2D
的实例,而不是
刚体
(或
刚体2d
)。然后,与前面相同的过程将引导我们找到
的根节点res://Bullet.tscn“
节点2d
的实例,但不是
刚体
(或
刚体2d


因此,请仔细检查
”res://Bullet.tscn“
。根节点的类型必须是
刚体
(或
刚体2d
),才能
应用脉冲


明确地说,如果您调用
apply\u impuse
,如下所示:

apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
self.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
或者像这样:

apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
self.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
然后在编写脚本的节点上调用它(在本例中是
kineticbody2d
,它没有
apply\u pulse
,因此不起作用)


重申一下,当您编写
object.function(…)
时,您正在对该
对象调用
function
。如果您编写了
self.function(…)
,那么您正在对包含您编写脚本的对象调用
function
。如果只编写
function(…)
,这与
self.function(…)
相同

为了以防万一,如果您使用
$
,我还将提到上述内容。例如,
$object.function(…)
将在名为“object”的子节点上调用
function


要使代码正常工作,调用函数的对象必须有它。

能否显示错误堆栈?