AddShape()错误

时间:2014-05-26 14:25:37

标签: javascript box2d

最近我开始学习Box2D引擎。我偶然发现了一个错误,无法找到原因。 来自firebug控制台: TypeError:groundBd.AddShape不是函数。 groundBd.AddShape(groundSd);

以下是我正在阅读的书中的代码 -js文件 -

    var carGame = { 
}

var canvas;
var ctx;
var canvasWidth; 
var canvasHeight;

$(function() {

    // create the world
    carGame.world = createWorld();

    // create the ground
    createGround();

    console.log("The world is created. ",carGame.world);

    // get the reference of the context
    canvas = document.getElementById('game');  
    ctx = canvas.getContext('2d');
    canvasWidth = parseInt(canvas.width);
    canvasHeight = parseInt(canvas.height);
});


function createWorld() {

    // set the size of the world
    var worldAABB = new b2AABB();
    worldAABB.minVertex.Set(-4000, -4000);
    worldAABB.maxVertex.Set(4000, 4000);

    // Define the gravity
    var gravity = new b2Vec2(0, 300);

    // set to ignore sleeping object
    var doSleep = false;

    // finally create the world with the size, graivty and sleep object parameter.
    var world = new b2World(worldAABB, gravity, doSleep);

    return world;
}

function createGround() {
    // box shape definition
    var groundSd = new b2BoxDef();
    groundSd.extents.Set(250, 25);
    groundSd.restitution = 0.4;

    // body definition with the given shape we just created.
    var groundBd = new b2BodyDef();
    groundBd.AddShape(groundSd);
    groundBd.position.Set(250, 370);
    var body = carGame.world.CreateBody(groundBd);

    return body;
}

-index file-

<!DOCTYPE html>
<html>
    <head>
        <meta chaset=utf-8>
        <title>Box2D Car Game</title>
        <link rel="stylesheet" type="text/css" href="css/cargame.css">
    </head>
    <body>
        <canvas id="game" width='1300' height='600' style=''></canvas>
        <script src="js/jquery-2.1.0.js"></script>
        <script src="js/protoclass.js"></script>

        <!-- box2djs -->
        <script src='js/box2d/common/math/b2Vec2.js'></script>
        <script src='js/box2d/common/math/b2Mat22.js'></script>
        <script src='js/box2d/common/math/b2Math.js'></script>
        <script src='js/box2d/common/b2Settings.js'></script>
        <script src='js/box2d/collision/b2AABB.js'></script>
        <script src='js/box2d/collision/b2Bound.js'></script>
        <script src='js/box2d/collision/b2BoundValues.js'></script>
        <script src='js/box2d/collision/b2Pair.js'></script>
        <script src='js/box2d/collision/b2PairCallback.js'></script>
        <script src='js/box2d/collision/b2BufferedPair.js'></script>
        <script src='js/box2d/collision/b2PairManager.js'></script>
        <script src='js/box2d/collision/b2BroadPhase.js'></script>
        <script src='js/box2d/collision/b2Collision.js'></script>
        <script src='js/box2d/collision/Features.js'></script>
        <script src='js/box2d/collision/b2ContactID.js'></script>
        <script src='js/box2d/collision/b2ContactPoint.js'></script>
        <script src='js/box2d/collision/b2Distance.js'></script>
        <script src='js/box2d/collision/b2Manifold.js'></script>
        <script src='js/box2d/collision/b2OBB.js'></script>
        <script src='js/box2d/collision/b2Proxy.js'></script>
        <script src='js/box2d/collision/ClipVertex.js'></script>
        <script src='js/box2d/collision/shapes/b2Shape.js'></script>
        <script src='js/box2d/collision/shapes/b2ShapeDef.js'></script>
        <script src='js/box2d/collision/shapes/b2BoxDef.js'></script>
        <script src='js/box2d/collision/shapes/b2CircleDef.js'></script>
        <script src='js/box2d/collision/shapes/b2CircleShape.js'></script>
        <script src='js/box2d/collision/shapes/b2MassData.js'></script>
        <script src='js/box2d/collision/shapes/b2PolyDef.js'></script>
        <script src='js/box2d/collision/shapes/b2PolyShape.js'></script>
        <script src='js/box2d/dynamics/b2Body.js'></script>
        <script src='js/box2d/dynamics/b2BodyDef.js'></script>
        <script src='js/box2d/dynamics/b2CollisionFilter.js'></script>
        <script src='js/box2d/dynamics/b2Island.js'></script>
        <script src='js/box2d/dynamics/b2TimeStep.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactNode.js'></script>
        <script src='js/box2d/dynamics/contacts/b2Contact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
        <script src='js/box2d/dynamics/contacts/b2CircleContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2Conservative.js'></script>
        <script src='js/box2d/dynamics/contacts/b2NullContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2PolyContact.js'></script>
        <script src='js/box2d/dynamics/b2ContactManager.js'></script>
        <script src='js/box2d/dynamics/b2World.js'></script>
        <script src='js/box2d/dynamics/b2WorldListener.js'></script>
        <script src='js/box2d/dynamics/joints/b2JointNode.js'></script>
        <script src='js/box2d/dynamics/joints/b2Joint.js'></script>
        <script src='js/box2d/dynamics/joints/b2JointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2Jacobian.js'></script>
        <script src='js/box2d/dynamics/joints/b2GearJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2GearJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2MouseJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>  

        <script src='js/html5games.box2dcargames.js'></script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

所以我能够找到为什么我得到了AddShape错误。 原因实际上是相当愚蠢的。 为了使AddShape工作,它需要b2BodyDef构造函数。 我使用了b2BoxDef()构造函数,用于定义框形状。 我知道这不是最好的答案,但我是box2d引擎的新手,所以我不能更好地回答它... 如果有人能给出更好的解释,那就不仅仅是欢迎了!