Debug Draw mask所有在BOX2D的cocos2d-JS中都不起作用,

时间:2018-04-25 06:33:57

标签: box2d cocos2d-js

我已经尝试了下面提到的所有代码,但这些代码都没有工作

    this._debugNode = new cc.PhysicsDebugNode(this.physicsWorld);
    this._debugNode.visible = true;
    this.addChild(this._debugNode, 10)

使用此版本时崩溃:

    var debugNode = new cc.PhysicsDebugNode(this.physicsWorld);
    debugNode.visible = true;
    this.addChild(debugNode);

我也试过这个,但它也没有提供任何输出。

    var debugDraw = new b2DebugDraw();
    debugDraw.SetSprite(cc.renderContext);
    debugDraw.SetDrawScale(32);
    debugDraw.SetFillAlpha(0.8);
    debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
    this.physicsWorld.SetDebugDraw(debugDraw);

1 个答案:

答案 0 :(得分:0)

  1. 我通过将此代码粘贴到更新方法来解决上述问题。

        var debugDraw = new Box2D.Dynamics.b2DebugDraw();
        debugDraw.SetSprite(document.getElementById("debugCanvas").getContext("2d"));
        cc.log( "this.m_ctx "+  document.getElementById("debugCanvas").getContext("2d"));
        debugDraw.SetDrawScale(30.0);
        debugDraw.SetFillAlpha(1.3);
        debugDraw.SetLineThickness(0.5);
        debugDraw.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit | Box2D.Dynamics.b2DebugDraw.e_jointBit);
        this.physicsWorld.SetDebugDraw(debugDraw);
        this.physicsWorld.DrawDebugData();
    
    1. 将此代码粘贴到index.html

        <canvas id="debugCanvas" width="1200" height="600" style="border:5px solid #FF0000;"></canvas>
        <script type="text/javascript">
       var debugCanvas = document.getElementById("debugCanvas");
       debugCanvas.width = 1200;
       debugCanvas.height = 600;
       debugCanvas.style.left = "50px";
       debugCanvas.style.top = "0px";
       debugCanvas.style.position = "absolute";
      </script>