WCK: Dynamically constructing a joint

I'm working on a platformer game, where the player's character needs to be able to hold on to an object, either to drag it somewhere or be dragged with it. So, when the player presses the grab button, I check the ContactLists for sensors positioned left and right of the character's solid geometry, to see if there's anything to grab. If there is, I want to construct a distance joint (or maybe revolute?) between the character and the object...

Code:
         var sensor:BodyShape = (direction < 0) ? left : right;
         var list:ContactList = contacts[sensor];
         list.clean();
         
         if (list.values.length > 0) {
            var contact:ContactEvent = list.values[0];
            var shape:BodyShape = contact.relatedObject as BodyShape;
            grabJoint = new Joint();
            grabJoint.type = "Distance";
            grabJoint.target1Object = middle;
            grabJoint.target2Object = shape;
            grabJoint.x = (middle.x + shape.x) * 0.5;
            grabJoint.y = (middle.y + shape.y) * 0.5;
            world.addChild(grabJoint);
         }


...but this has absolutely no effect whatsoever-- no errors are thrown, and the joint does not function. Inspecting the value of shape, reveals that it's frequently an object that the character is nowhere near, although even when it is the correct object the joint doesn't function.
Try this instead:
Code:
grabJoint.bodyShape1 = middle;
grabJoint.bodyShape2 = shape;


I'm going to fix it in the next commit so setting target1Object will also work. I guess I intended bodyShape1/2 to be used in code, but there's no way to set the second target as a joint if you want to (this doesn't effect you).
Great! I can successfully make a joint that works as expected. Thank you.

I still have an issue with the results in my ContactList. Sometimes, the shape that appears in that list is nowhere near the character, and the object that should be grabbed, isn't even in the ContactList. Any ideas?
...ah, looks like the same problem sketchbookgames is dicussing in this thread:
viewtopic.php?f=8&t=5649

...I'll tune in over there for the subject of ContactLists.

Original thread: https://box2d.org/forum/viewtopic.php?t=5979