HaxeFlixel。颈背。距离联合调试。将精灵拖到区域

HaxeFlixel. Nape. DistanceJoint debugging. Drag sprite to area

我有一个可以在屏幕上四处拖动的精灵。我希望能够将这个精灵拖到一个区域(框)中。现在只能把精灵丢进盒子里,直接拖到客栈里,程序就崩溃了。

我已经使用 Adob​​e Flash 调试器在 FlashDevelop 中进行了调试。当我将精灵放入盒子中时,调试器指向 DistanceJoint.hx 文件中的这一行代码:

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";

我想我明白我必须做什么,但我很难找到一种方法来在我的 Drag class.

中进行适当的例外处理

我的想法是在Playstate中使用collideLightBox函数时,将Drag中的mouseJoint打断class。但我不知道该怎么做,或者这是否是正确的想法。请帮忙。

相关代码:

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;
    }
}

给 Drag 一个类似 "destroyConstraints()" 的函数,并在该函数中设置 mouseJoint.space = null。在collideLightBox中调用drag.destroyConstraints().