I have a quadtree which has a remove method, and every time I remove an item from the game I call this method. The problem is that in the foreach loop, the child variable is null. How can I bypass this method so if the variable is null it won't give me an error?
private void CollapseChildren(RectangleTreeNode node)
{
foreach (RectangleTreeNode child in node.Children)
{
if (child == null)
{
return;
}
while (child.Items.Count > 0)
{
MoveUp(child.Items[0]);
}
}
node.Children = null;
}
Here's the remove method.
private void Remove(RectangleTreeNode node, RectangleTreeItem item)
{
node.Items.Remove(item);
item.Parent = null;
while (node != null)
{
node.ItemCount--;
if (node.ItemCount < 6)
{
CollapseChildren(node);
}
node = node.Parent;
}
}
If the value is null then I shouldn't have to loop through the children in nodes.Children because if I do loop through then I get the error at the child variable. Meaning the node doesn't have a child.
node.Children = null, always create an empty collection likenode.Children = new RectangleTreeNode[0]. This avoids errors and simplifies logic considerably