programmaticly add Physicsprite on view_loaded

Feb 12, 2013 at 8:50 AM
Hi,

This is my gameworld:
Image

The lines are just Line objects and i need them to be static Sprite object so the little ball in the center cant fall out of the world anymore.

this is my code for creating the sprite objects:
        private PhysicsSprite createSprite(string name, double previousPointX, double previousPointY, double nextPointX, double nextPointY)
        {
            //create sprite
            PhysicsSprite sp = new PhysicsSprite()
            {
                Name = name,
                RenderTransform = new TranslateTransform()
                {
                    X = previousPointX,
                    Y = previousPointY
                },
                IsStatic = true
            };

            sp.Height = Math.Abs(previousPointY - nextPointY);
            sp.Width = Math.Abs(previousPointX - nextPointX);

            Logger.Log(string.Format("PhysicsSprite {0} height: {1} width:{2}", name, sp.Height, sp.Width));

            //create Line element
            Line line = new Line();
            line.X1 = previousPointX;
            line.Y1 = previousPointY;
            line.X2 = nextPointX;
            line.Y2 = nextPointY;

            //set line color
            line.Stroke = new SolidColorBrush()
            {
                Color = Color.FromArgb(255, 255, 255, 255)
            };

            Canvas.SetZIndex(line, 4);
            sp.Children.Add(line);
            return sp;

        }
and this is how i try to add it to the world:
PhysicsSprite sp = createSprite("name" + step.ToString(), previousPointX, previousPointY, pointX, pointY);
this.canvasWorld.AddPhysicsSprite(sp, true);
Above aproach leads to an Nullreference exception in the Update() method in PhysicsSprite.cs. The property BodyObject is NULL.
What is the correct approach of adding static physicsSprite on runtime?
Coordinator
Feb 12, 2013 at 11:19 AM
Where are you calling createSprite from?

Make sure it is in the Loaded event, or after the Loaded event for the usercontrol containing your main Game Canvas.
Feb 12, 2013 at 11:37 AM
GameTwo(view).Loaded --->createBounds-->
 for (double step = 0; step <= (2 * Math.PI); step += 0.1)
{
.......
......
......
PhysicsSprite sp = createSprite("name" + step.ToString(), previousPointX, previousPointY, pointX, pointY);
this.canvasWorld.AddPhysicsSprite(sp, true);
}
so i wait for the view to be loaded and then add the additional sprites
Mar 7, 2013 at 3:08 PM
for the people who read this.

In the end i just used the same structure as Andy did in the Demo.Advanced for adding controls