Intermittent Error: "You must add a PhysicsController Behavior to the Canvas representing the main Container."

Sep 4, 2012 at 12:19 AM

Hi Andy,

Getting an intermittent error after instantiating a number of objects, this is usually after creating 10 or so objects using the same code path so i assume it's got something to do with the timings.

I've got a Usercontrol with a Physics Object Behaviour attached, which i'm adding to the game page with:

 

var test = _physicsController.AddPhysicsBodyForCanvasWithBehaviors(newBubble.LayoutRoot);

 

This is the error i'm getting, is there any way to avoid or misimise this? 

"You must add a PhysicsController Behavior to the Canvas representing the main Container."

at Spritehand.PhysicsBehaviors.PhysicsObjectBehavior.AssociatedObject_Loaded(Object sender, RoutedEventArgs e)   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Coordinator
Sep 4, 2012 at 2:32 AM

This error occurs because the PhysicsController cannot be found (the method FindController in PhysicsControllerMain.cs is what is failing to find it).

Usually I've seen this error when I've placed a PhysicsObjectBehavior on an element that is NOT directly under the Canvas with the PhysicsControllerBehavior.

So I would take a look at where you have your PhysicsObjectBehavior's - they should be on elements that are direct children with the "Game Canvas" (the Canvas with the PhysicsControllerBehavior.

Note that you can group visual elements into a Canvas, and then apply the PhysicsObjectBehavior to that Canvas for cases where you have multiple visual elements representing a single "sprite".