最小 Matter.js 示例:Engine.run 不是函数

Minimal Matter.js example : Engine.run is not a function

我从 Matter.js 库的 getting started section 中复制了代码,但出现了 Uncaught TypeError:“Engine.run 不是一个函数”. 我在网上到处搜索,但没有任何帮助。作为预防措施,我在加载所有内容后向 运行 代码添加了一个事件侦听器。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.9.2/matter.min.js" type="text/javascript"></script>
    <title>Matter.js example</title>
</head>
<body>
    <script type="text/javascript">
        window.addEventListener("load",init);

        function init() {
            // module aliases
            var Engine = Matter.Engine,
                Render = Matter.Render,
                Bodies = Matter.Bodies,
                Composite = Matter.Composite;

            // 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 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
            Composite.add(engine.world, [boxA, boxB, ground]);

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

            // run the renderer
            Render.run(render);
        }
    </script>
</body>
</html>

拜托,我做错了什么?

您使用的 matter-js 库版本错误,请尝试使用最新版本。我刚刚尝试使用 0.15.0,它工作正常。

matter-js >= 0.10.0 得到支持 Render.run()。请确保您使用的版本支持的功能。

window.addEventListener("load",init);

        function init() {
            // module aliases
            var Engine = Matter.Engine,
                Render = Matter.Render,
                Runner = Matter.Runner,
                Bodies = Matter.Bodies,
                Composite = Matter.Composite;

            // 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 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
            Composite.add(engine.world, [boxA, boxB, ground]);

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

            // run the renderer
            Render.run(render);
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.17.0/matter.min.js" type="text/javascript"></script>
    <title>Matter.js example</title>
</head>
<body>
</body>
</html>