【发布时间】: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