如何做出物质的最小例子?js有效吗?

时间:2015-01-21 23:58:32

标签: javascript html5 canvas physics-engine

我正在尝试使用matter.js physics library。我正在使用他们的"getting started" tutorial,但找不到画布。

这是我的HTML:

<html>
<head>
<meta charset="UTF-8">
<title>Physics test</title>
</head>
<script type="text/javascript" src="./lib/matter-0.8.0.js"></script>
<script type="text/javascript">
// Matter.js module aliases
var Engine = Matter.Engine,
    World = Matter.World,
    Bodies = Matter.Bodies;

// create a Matter.js engine
var engine = Engine.create(document.body);

// create two boxes and a ground
var boxA = Bodies.rectangle(400, 200, 80, 80);
var boxB = Bodies.rectangle(450, 50, 80, 80);
var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

// add all of the bodies to the world
World.add(engine.world, [boxA, boxB, ground]);

// run the engine
Engine.run(engine);
</script>
<body>
</body>
</html>

控制台显示以下错误:

" [Matter] warn: No "render.element" passed, "render.canvas" was not inserted into document."

我试图创建render.element和render.canvas,但我是盲目地行事。一个“让你开始的最小例子”应该已经有效了。我做错了什么?

3 个答案:

答案 0 :(得分:5)

逐个部分演示,我发现大多数代码应该在一个函数中,在页面加载中调用,如:

<body onload='Start()'>

function Start() {
    // create a Matter.js engine
    var engine = Engine.create(document.getElementById('canvas-container'));

    // create two boxes and a ground
    var boxA = Bodies.rectangle(400, 200, 80, 80);
    var boxB = Bodies.rectangle(450, 50, 80, 80);
    var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

    // add all of the bodies to the world
    World.add(engine.world, [boxA, boxB, ground]);

    // run the engine
    Engine.run(engine);
}

答案 1 :(得分:2)

浏览器控制台中存在document.body,但JavaScript中不存在。

您可以改为使用document.querySelector("body")

错误可能是因为您将undefined传递给Engine.create(),因为document.body返回undefined

另外,请确保在window.onload事件后执行代码,以便加载所有 HTMLElements 。像这样:

window.addEventListener("load",init);
function init(){
    var body = document.querySelector("body");
    // Matter.js module aliases
    var Engine = Matter.Engine,
    World = Matter.World,
    Bodies = Matter.Bodies;

    // create a Matter.js engine
    var engine = Engine.create(body);

    // create two boxes and a ground
    var boxA = Bodies.rectangle(400, 200, 80, 80);
    var boxB = Bodies.rectangle(450, 50, 80, 80);
    var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

    // add all of the bodies to the world
    World.add(engine.world, [boxA, boxB, ground]);

    // run the engine
    Engine.run(engine);
}

希望这有帮助!

答案 2 :(得分:1)

这个问题需要很长时间,但我发现可以使用新的Render对象改进库,因此新的基本示例代码如下

<head>代码

中加入以下内容
<script src="matter.js" type="text/javascript"></script>

JS代码可以放在<body>标记

的底部
<script>

    // module aliases
    var Engine = Matter.Engine,
        Render = Matter.Render,
        World = Matter.World,
        Bodies = Matter.Bodies;

    // create an engine
    var engine = Engine.create();

    // create a renderer
    var render = Render.create({
        element: document.body,
        engine: engine
    });

    // create two boxes and a ground
    var ball = Bodies.circle(420, 15, 20);

    var boxA = Bodies.rectangle(400, 200, 80, 80);
    var boxB = Bodies.rectangle(450, 50, 80, 80);
    var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

    // add all of the bodies to the world
    World.add(engine.world, [ball, boxA, boxB, ground]);

    // run the engine
    Engine.run(engine);

    // run the renderer
    Render.run(render);

</script>