views:

95

answers:

2

Hi all,

I am starting to learn silverlight and to practice I am doing a simple space invaders type videogame.

My issue is I am creating custom controls (bullets) programatically like so:

        if(shooting)
        {
            if(currBulletRate == bulletRate)
            {
                Bullet aBullet = new Bullet();

                aBullet.X = mouse.X - 5;
                aBullet.Y = mouse.Y - Ship.Height;
                aBullet.Width = 10;
                aBullet.Height = 40;
                aBullet.Tag = "Bullet";

                LayoutRoot.Children.Add(aBullet);

                currBulletRate = 0;
            }
            else
                currBulletRate++;
        }

However I am having trouble removing them once they go off bounds (leave the LayoutRoot).

I tried looping throught the LayoutRoot.Children and removing but I can't seem to get it right.

Any insight would be much appreciated!

A: 

This is a code segment I found online but I must be doing something wrong.

        UIElement[] tmp = new UIElement[LayoutRoot.Children.Count];             
        LayoutRoot.Children.CopyTo(tmp, 0);  

        foreach (UIElement aElement in tmp)
        {
            Shape aShape = aElement as Shape; 

            if (aShape != null && aShape.Tag != null)
            {

                if (aShape.Tag.ToString().Contains("Bullet"))
                {
                    if (Canvas.GetTop(aShape) < 0)
                    {
                        LayoutRoot.Children.Remove(aElement);
                    }
                }
            }
        }
Mayo
@Mayo, when you want to add something to your question, (something which is not the solution to your problem) it's better to edit your original question.
TimothyP
+1  A: 
UIElement[] tmp = new UIElement[LayoutRoot.Children.Count];             
LayoutRoot.Children.CopyTo(tmp, 0);  

foreach (UIElement aElement in tmp)
{
    Shape aShape = aElement as Shape; 

    if (aShape != null && aShape.Tag != null)
    {

        if (aShape.Tag.ToString().Contains("Bullet"))
        {
            if (Canvas.GetTop(aShape) + aShape.ActualHeight < 0) // This checks if it leaves the top
            {
                LayoutRoot.Children.Remove(aElement);
            }
            else if(Canvas.GetTop(aShape) > Canvas.ActualHeight) // This condition checks if it leaves the bottom
            {
                LayoutRoot.Children.Remove(aElement);
            }
        }
    }
}

The code you pasted was only checking if the bullet left the top of the canvas.

Stephan
Ok I manage to get it to work. The bullets are being removed, but... Now it creates a lot of overhead making it pretty slow everytime it has to remove a control.
Mayo
Nvm my last comment. I now understand why the elements in LayoutRoot.Children are copied to a temporary array of UIElements. Apparently looping directly through the children in a LayoutRoot is too slow. I tried simplifying the code by removing this part but adding it back solved my problem.Thanks for the help!
Mayo
If this solves your issue please accept it as an answer
Stephan