Repository navigation
Components
inventivetalent edited this page May 17, 2016
·
2 revisions
Components should always be added in the init() function.
menu.addComponent("<File Name>", x, y, width, height, [initArgs]);
This adds a component in a file.
-
File nameis the component file name, without the.jsextension. -
x&yare the position of the component. -
width&heightare the component dimensions. -
initArgsare optional arguments passed to the component'sinitfunction. - Example:
menu.addComponent("Button", 10, 10, 32, 64);
menu.addComponent(x, y, width, height, <anonymous component object>);
- Here, the last parameter is an anonymous object. See the details below for more information.
Similar to the menu, components also have init, click & render functions.
Unlike the menu functions, component functions have to be declared inside of an Object, for example:
{
init: function() {
}
}
init(initArgs) {
}
This is called when the component is added.
-
initArgsare arguments specified by the menu in theaddComponentfunction.
click(player, relativePosition, absolutePosition, action) {
}
Called when a player clicks this component.
-
relativePositionis the clicked position on the component. -
absolutePositionis the absolute clicked position on the menu. -
actionis either 0 for right-click, or 1 for left-click.
render(graphics, player) {
}
Called to render this component.
Anonymous components allow you to create the whole menu and its components in a single file.
Example:
menu.addComponent(10, 10, 20, 30, {
init: function(args) {},
render: function(graphics, player) {
// Do render stuff
}
});
File Components are basically the same as anonymous components, but since they are in their own file, multiple Menus can use them.
The only code difference to anonymous components, is that they need to be wrapped in parentheses:
({
init: function(args) {},
render: function(graphics, player) {
// Do render stuff
}
});