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.