【发布时间】:2019-12-22 05:58:31
【问题描述】:
我的游戏设置中有一个 Kinetic Body 2D,用于在需要时在字典中显示不同类型的精灵。为此,我使用了这段代码并且成功了:
get_node("Pineapple").get_node("Display").set_texture(global_const.pineapple)
变量pineapple 在global_const.gd 单例脚本中以这种形式出现-
const pineapple = preload("res://Ingredients/Pineapple.png")
后来,我决定完全摆脱使用“global_const”变量,因为我为我想要显示的所有项目构建了一个字典。新字典现在看起来像这样 -
const ingredients = [{iname="Pineapple", icon="res://Ingredients/pineapple.png"},
{iname="Cherry", icon="res://Ingredients/cherry.png"},
{iname="Banana", icon="res://Ingredients/banana.png"}]
我想修改我使用set_texture 的代码,我这样做了 -
for item in on_scene_ingredients: #this loops through all the instances of my Kinetic Body 2D.
#they have different values in "iname" to differentiate between each other
for reference in dictionary.ingredients: #this checks all the items in my library
if item.iname==reference.iname: #checks for common name to assign the desired icon
item.get_node("Display").set_texture(reference.icon)
我测试了所有变量是否正确以及逻辑是否存在任何缺陷,但没有。 导致问题的唯一行是 -
item.get_node("Display").set_texture(reference.icon)
Godot 引发运行时错误 - 基础“Sprite”中的“set_texture”函数中的类型无效。无法将参数 1 从 String 转换为 Object。
我该如何解决这个问题?我哪里错了?
【问题讨论】: