Exception related to removing objects

Aug 22, 2010 at 8:30 PM
Edited Aug 22, 2010 at 8:30 PM

I have a game started where a ship is shooting, and being shot at ( brilliant idea, huh?  )

I am periodically getting this error:

System.Exception was unhandled
  Message=You must add a PhysicsController Behavior to the Canvas representing the main Container.
       at Spritehand.PhysicsBehaviors.PhysicsObjectBehavior.AssociatedObject_Loaded(Object sender, RoutedEventArgs e)
       at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

I believe it is because of how I am trying to remove things from the game. The ship and enemies both 'shoot. The shots are user controls. I use the collission event only on the shot:

projectileSprite.Collision += new PhysicsSprite.CollisionHandler(projectileSprite_Collision);
        void projectileSprite_Collision(string sourceSprite, string collisionSprite)
            if (_physicsController.PhysicsObjects.ContainsKey(sourceSprite))
                Debug.WriteLine("removing " +  sourceSprite);
                PhysicsSprite physicsSprite = _physicsController.PhysicsObjects[sourceSprite];
                physicsSprite.IsActive = false;
Any thoughts?
Aug 23, 2010 at 11:01 AM

The error appears to be from adding a new object in, not from the removal. If you do a search on this forum for the word "programmatically", you should see some posts on adding elements programmatically (also the Rag Doll Demo in the download shows this).