Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | insaneNic |
I have a Main
node and a Scene called Block
. I want to add multiple Blocks into the Main scene. Main
does not have any Blocks as Children by default. The relevant code in Main.cs
is:
[Export]
public PackedScene BlockScene {get; set;}
...
for (int i = 0; i < 8; i++)
{
Block block = BlockScene.Instantiate<Block>();
Vector2 spawnPosition = new Vector2(
x: (float)(12 + i*8),
y: (float)(128)
);
block.Start(spawnPosition);
AddChild(block);
}
in Block.cs
I have
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
animatedSprite.Animation = "default";
animatedSprite.Play();
Hide();
}
public void Start(Vector2 pos){
Position = pos;
Show();
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
}
I know that the Blocks are added as Children to Main because I have printed all of the children of Main and they are there. I have also double checked that I have added the right scene to BlockScene
. I have turned on Visible Collision Shapes and also don’t see anything. I don’t know what else I could possibly be missing. Thanks for your help.