I'm getting the following error after installing and running Matter-js for the first time in Node.js. The issue seems to be occurring on Engine.create().
/***/node_modules/matter-js/build/matter.js:4527
if (window.performance) {
^
ReferenceError: window is not defined
at Object.Common.now (/***/node_modules/matter-js/build/matter.js:4527:9)
at Timeout._onTimeout (/***/node_modules/matter-js/build/matter.js:6112:33)
at ontimeout (timers.js:424:11)
at tryOnTimeout (timers.js:288:5)
at listOnTimeout (timers.js:251:5)
at Timer.processTimers (timers.js:211:10)
I've tried a suggestion from another post saying to add global.window = {}; to get around this issue, but when I tried that I got the following error on Engine.update(this.engine, 1000/60).
/***/node_modules/matter-js/build/matter.js:4937
var world = engine.world,
^
TypeError: Cannot read property 'world' of undefined
at Object.Engine.update (/***/node_modules/matter-js/build/matter.js:4937:28)
at Timeout._onTimeout (/***/game.js:59:24)
at ontimeout (timers.js:424:11)
at tryOnTimeout (timers.js:288:5)
at listOnTimeout (timers.js:251:5)
at Timer.processTimers (timers.js:211:10)
In the first instance it could be that you're using Matter.Runner? It's possible that it has an issue in node so I'd advise calling Engine.update in your own loop. As for your second issue are you sure that this.engine is defined as an engine instance before you call Engine.update?
I'm pretty sure. Here's how I'm starting and updating. Maybe the second error is a problem in how I'm referencing 'this' inside a function? I'll look into that...
server.js
var game = new Game();
game.startGameLoop(function() {
io.sockets.emit('state', game.getGameState());
});
game.js
global.window = {};
function Game() {
this.boardSize = 10000.0;
this.players = {};
this.engine = Engine.create(); //first error
}
Game.prototype.startGameLoop = function startGameLoop(fn) {
setInterval(function() {
Engine.update(this.engine, 1000 / 60); //second error (when I include global.window = {};)
fn();
}, 1000 / 60);
}
Game.prototype.getGameState = function getGameState() {
return new GameState(this);
}
Both issues fixed. It was an issue on my end.
Changed this code:
Game.prototype.startGameLoop = function startGameLoop(fn) {
setInterval(function() {
Engine.update(this.engine, 1000 / 60); //second error (when I include global.window = {};)
fn();
}, 1000 / 60);
}
to this:
Game.prototype.startGameLoop = function startGameLoop(fn) {
var engine = this.engine;
setInterval(function() {
Engine.update(engine, 1000 / 60);
fn();
}, 1000 / 60);
}
Most helpful comment
Both issues fixed. It was an issue on my end.
Changed this code:
to this: