Skip to content

Instantiating Objects

whirlibulf edited this page May 8, 2013 · 3 revisions

Procedural

The two main functions you want to use are createObject and addComponentToObject:

game.createObject('object id');
game.addComponentToObject('object id', 'position', {'x': 0, 'y': 12});
game.addComponentToObject('object id', 'script', {'scripts': [require('some-script')]});

First an empty object is created, then components are added to it one by one.

Data-driven

Another way to instantiate new game objects is to pass a 'template' to the createObject function:

game.createObject('object id', {
  position: {
    'x': 0,
    'y': 12
  },
  script: {
    scripts: [require('some-script')]
  }
});

If you store the template in a variable, you can re-use it to create many objects with the same set of components and options:

var template = {
  //component options
};
game.createObject('object 1', template);
game.createObject('object 2', template);
game.createObject('object 3', template);