Skip to main content

templates


templates

This object allows you to create new Copies and manipulate them.

templates.copy(template, x, y, exts)

Creates a new Copy of a given template. template must be a string — the name of the template. If x or y is omitted, they are set to 0. Returns the newly created copy.

The extensions object can be used to add parameters that will be available in a Copy's OnCreate event.

Note

By default, this method puts the new copy in the current main room (rooms.current). If you want to create a copy in, say, a UI room that is stacked on top of your main room, see the templates.copyIntoRoom method below.

Example: Create a bullet at the current copy's position and send it in a particular direction

JavaScript
var bullet = templates.copy('Bullet', this.x, this.y);
bullet.direction = this.direction;

Example: Spawn a copy under a cursor on mouse click

You will need an action called Press that reacts to pointer's primary button. Read more about actions here.

JavaScript
if (actions.Press.down) {
    templates.copy('Fruit', pointer.x, pointer.y);
}

templates.copyIntoRoom(template, x, y, parentRoom, extensions)

An advanced form of templates.copy that puts a copy into a particular parentRoom. Other arguments match the ones used in templates.copy.

Example: Create a copy in a layered room "UI_Layer" with additional parameters

JavaScript
var uiLayer = rooms.list['UI_Layer'][0];
if (uiLayer) {
    templates.copyIntoRoom('UI_Message', 35, 65, uiLayer, {
        message: 'Your warriors have engaged the enemy!',
        type: 'alert'
    });
}

templates.each(func: Function)

Applies a function to all the active copies.

Example: destroy all the copies within a 150px radius

JavaScript
var me = this;
templates.each(function () {
    if (this !== me) { // aren't we trying to destroy ourselves?
        if (u.pdc(this.x, this.y, me.x, me.y) <= 150) {
            this.kill = true;
        }
    }
});

Tips

u.pdc computes distance between two points. This and other similar functions can be found here.

templates.exists(templateName)

Checks whether there are any copies of this template's name. Will throw an error if you pass an invalid template name. Returns true if at least one copy exists in a room; false otherwise.

templates.valid(obj)

Checks whether a given object exists in game's world. Intended to be applied to copies, but may be used with other PIXI entities. Returns true if a copy exists and is not marked for removal; false otherwise.

templates.isCopy(obj)

Checks whether a given object is a ct.js copy. Returns true if the passed object is a copy; false otherwise.

templates.list['TemplateName']

Returns an array with all the existing copies of the specified template.

Example: make an order to destroy all the 'Bonus' Copies

JavaScript
for (var bonus of templates.list['Bonus']) {
    bonus.kill = true;
}

templates.withCopy(copy: Copy, func: Function)

Works like templates.each, but only for the specified Copy.

templates.withTemplate(template: string, func: Function)

Works like templates.each, but applies a function to every copy of the specified template.