为什么我的精灵没有出现在Godot引擎的场景中,即使我使用脚本创建了它?

问题描述 投票:0回答:1
using Godot;
using System;

public partial class PlayerCharacter : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {  
        Sprite2D Player = new Sprite2D();
        Texture spriteTexture (Texture)ResourceLoader.Load("res://Main/Assets/ball.png");
        Player.Texture = (Texture2D)spriteTexture;
        Player.Position = new Vector2(100,100);
    }


    public override void _Process(double delta)
    {
    }
}

这是我显示精灵的代码。

enter image description here

这就是显示的内容

c# game-engine godot
1个回答
0
投票

您需要使用

AddChild
将新实例添加到场景树中。例如:

AddChild(Player);

尽管如此,作为一个孩子,我并不清楚你想要做什么。例如,您可能希望它作为创建它的节点的同级节点:

GetParent().AddChild(Player);

添加它的位置可能很重要,因为默认情况下,节点会随其父节点移动(即,当父节点移动时,其子节点也会移动)。


无论如何,我看到您正在

_Ready
中执行此操作,这让我认为也许您可以在编辑器中添加节点,而不是从代码中执行此操作。或者,您可以创建一个场景并实例化它(是的,您仍然需要使用
AddChild
将实例添加到场景树中)。

我不知道你在做什么,但为了以防万一,我会提醒你:

Sprite2D
只关心图形,不关心物理(所以它不会检测碰撞)。因此,我们通常会使用带有图形节点的物理体作为智利(因此当物理体移动时图形节点也会移动)。

© www.soinside.com 2019 - 2024. All rights reserved.