0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-21 09:06:50 -05:00
4 Running
liabru edited this page 2021-04-15 00:48:07 +01:00

Examples of how to run a Matter.js engine

  1. Runner example
  2. Using Matter.Runner
    1. Documentation
    2. Usage
    3. Options

Runner example

Browser

A basic example for running a previously created Matter.Engine (see Getting started):

(function run() {
    window.requestAnimationFrame(run);
    Engine.update(engine, 1000 / 60);
})();

See also the source of Matter.Runner that provides some more advanced features.

Node

When using node you can use setInterval instead of window.requestAnimationFrame:

setInterval(function() {
    Engine.update(engine, 1000 / 60);
}, 1000 / 60);

Using Matter.Runner

There is an included runner called Matter.Runner. This module is an optional utility which provides a game loop, that handles continuously updating a Matter.Engine for you. It is intended for development and debugging purposes, but may also be suitable for simple games.

Documentation

See the documentation for Matter.Runner.

Usage

The simplest way is to use the Runner.run helper:

var engine = Engine.create();
Runner.run(engine);

Alternatively you can create the runner first:

var engine = Engine.create();
var runner = Runner.create();
Runner.run(runner, engine);

Options

A number of options may be passed to Matter.Runner.create:

var runner = Runner.create({
    delta: 1000 / 60,
    isFixed: false,
    enabled: true
});