对于为什么这段代码不能工作,有什么见解吗?
当我右键单击时,游戏崩溃并给出错误:“无效调用。不存在函数'play‘in base 'Array'”。
func _ready():
anim_Play = get_tree().get_nodes_in_group("AnimationPlayer") func_input(event):
if Input.is_action_pressed("aim"):
anim_Play.play("AimSights")发布于 2019-10-22 19:38:13
我猜从您的代码中可以看出,您正在尝试获取对AnimationPlayer节点的引用,但它失败了,您得到了一个数组。
这是因为您使用的是get_nodes_in_group (返回组中的节点数组),而不是返回节点的get_node。
无效调用。不存在的函数'play‘in base 'Array
表示您正在尝试从不存在的数组对象调用play方法(在AnimationPlayer中找到)。
你会得到像这样的AnimationPlayer
var anim_Play = get_node("./path/to/your/AnimationPlayer")发布于 2019-10-25 14:10:21
回答您的问题
get_nodes_in_group(group)返回同时位于SceneTree和组group中的节点的Array。
假设在" AnimationPlayer“组中有一个AnimationPlayer节点。我们将像这样获取它:
var anim_player = get_tree().get_nodes_in_group("AnimationPlayer")[0]请注意[0]。这被称为存取器。我们在元素0处访问数组。现在,我们可以调用play:
anim_player.play("AimSights")注意:访问数组中不存在的元素是错误的。
推荐
这似乎是对组的不恰当使用。如果动画播放器与脚本在同一场景中,我建议您使用节点路径,就像svarog建议的那样。
此外,它将有助于阅读或搜索一些基本的编程概念:特别是对象和数组。
最后,阅读Godot文档中的scenes and nodes页面:https://docs.godotengine.org/en/3.1/getting_started/step_by_step/scenes_and_nodes.html
Godot文档的整个入门指南是学习Godot的宝贵资源。它会对你有很大的帮助,而且读起来不会太长。
祝好运!
https://stackoverflow.com/questions/58479949
复制相似问题