【问题标题】:Custom loop in Matter.jsMatter.js 中的自定义循环
【发布时间】:2015-09-09 03:42:33
【问题描述】:

我想在我的自定义游戏循环中使用 Matter.js,我按照 here 的说明添加了 Engine.update(engine, 1000/60, 1);在绘制循环中,但没有任何反应,只是空白屏幕。我需要在某处添加tick方法吗?

这是我的代码:

window.onload = function () {

    // 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);

    draw();

    function draw(){ 
        Engine.update(engine, 1000/60, 1);        

        requestAnimationFrame(draw);
    }
};

【问题讨论】:

  • 你有想过这个问题吗?
  • 根据这段代码你预计会发生什么?您已经初始化了一些没有渲染器的 matter.js 对象,然后启动了一个不渲染任何内容的自定义循环。 MJS 引擎正在运行您看不到的模拟。尝试添加一些渲染代码,通过在画布、DOM、p5.js 等上绘制它们来反映 MJS 对主体所做的事情。有关 DOM 示例,请参阅Using Matter.js to render to the DOM or React

标签: matter.js


【解决方案1】:

您可能使用了错误的参数。 我有一个使用Engine.update 的函数,看起来像这样:

var updateTime = 1000/60;
Matter.Engine.update(engine, [delta=updateTime], [correction=1]);

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多