1

I have a sprite that I can drag around on screen. I want to be able to drag this sprite into an area (box). As it stands now I can only drop the sprite into the box, but when I drag it directly inn, the program crashes.

I have debugged in FlashDevelop using the Adobe Flash debugger. When I place the sprite into the box the debugger points to this line of code in the DistanceJoint.hx file:

if(b1.space!=space||b2.space!=space)throw "Error: Constraints must have each body within the same space to which the constraint has been assigned";

I think I understand what I have to do, but I am having a hard time finding a way to make a proper exception in my Drag class.

My idea is to break the mouseJoint in the Drag class when the collideLightBox function in Playstate is used. But I do not know how to do that, or if it is the right idea. Please help.

Relevant code:

class Drag extends FlxGroup {

    var mouseJoint:DistanceJoint;

    public inline function registerPhysSprite(spr:FlxNapeSprite)
    {
        MouseEventManager.add(spr, createMouseJoint);

    }

    function createMouseJoint(spr:FlxSprite)
    {

        var body:Body = cast(spr, FlxNapeSprite).body;

        mouseJoint = new DistanceJoint(FlxNapeState.space.world, body,
            new Vec2(FlxG.mouse.x, FlxG.mouse.y),
            body.worldPointToLocal(new Vec2(FlxG.mouse.x, FlxG.mouse.y)),
            0, 0);

        mouseJoint.space = FlxNapeState.space;
    }


    override public function update():Void
    {
        super.update();

        if (mouseJoint != null)
        {
            mouseJoint.anchor1 = new Vec2(FlxG.mouse.x, FlxG.mouse.y);

            if (FlxG.mouse.justReleased)
            {
                mouseJoint.space = null;
            }
        }
     }
}

class PlayState extends FlxNapeState {
    override public function create()
    {
    super.create();
    bgColor = FlxColor.BLACK;
    napeDebugEnabled = true;

    var light = new Light(10, 10);
    var box = new Box(100, 100);
    var drag:Drag;

    createWalls(1, 1, 1024, 768, 10, new Material(1, 1, 2, 1, 0.001));

    add(light);
    add(box);

    drag = new Drag();
    add(drag);                                                         
    drag.registerPhysSprite(light);

    light.body.velocity.y = 200;

    FlxNapeState.space.listeners.add(new InteractionListener(
        CbEvent.BEGIN, 
        InteractionType.COLLISION, 
        Light.CB_TYPE,
        Box.CB_TYPE,
        collideLightBox));
    }

    function collideLightBox(callback:InteractionCallback)
    {
        var light:Light = cast callback.int1.castBody.userData.sprite;
        light.kill();
    }
}

class Light extends FlxNapeSprite {
    public static var CB_TYPE(default, null) = new CbType();

    public function new(x:Float, y:Float)
    {
        super(x, y);
        makeGraphic(10, 10, FlxColor.TRANSPARENT);
        var radius = 5;
        drawCircle(5, 5, radius, FlxColor.WHITE);
        createCircularBody(radius);
        body.cbTypes.add(CB_TYPE);

        body.userData.sprite = this;
    }
}

class Box extends FlxNapeSprite {
    public static var CB_TYPE(default, null) = new CbType();

    public function new(x:Float, y:Float)
    {
        super(x, y);
        makeGraphic(100, 50, FlxColor.GREEN);
        createRectangularBody(width, height);
        body.cbTypes.add(CB_TYPE);
        body.type = BodyType.STATIC;
    }
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
ibliso
  • 33
  • 4
  • I'm not sure if this is the problem, but you should not be able to put a sprite with a physics body inside other sprite with a physics body, that goes against the physics engine physics. – TiagoLr May 02 '15 at 12:37

1 Answers1

0

Give Drag a function like "destroyConstraints()" and set mouseJoint.space = null inside that function. In collideLightBox, call drag.destroyConstraints().

MSGhero
  • 411
  • 2
  • 7
  • I get this error: Error #1009: Cannot access a property or method of a null object reference. When I debug it points to the drag.destroyConstraints() function. I tried to wrap it in an 'if' statement to check if mouseJoint.space != null, but that gave the same error. – ibliso May 08 '15 at 20:16
  • Maybe the drag object is null. Anything I say is guessing, you'll have to debug your own project and see what the problem is. – MSGhero May 08 '15 at 21:08
  • It works!! I just had to move the function below update(). Thank you so much! – ibliso May 09 '15 at 15:11