Skip to content

Components

inventivetalent edited this page May 17, 2016 · 2 revisions

Components

Adding components

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 name is the component file name, without the .js extension.
  • x & y are the position of the component.
  • width & height are the component dimensions.
  • initArgs are optional arguments passed to the component's init function.
  • 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.

Basic Functions

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

init(initArgs) {
}

This is called when the component is added.

  • initArgs are arguments specified by the menu in the addComponent function.

click

click(player, relativePosition, absolutePosition, action) {
}

Called when a player clicks this component.

  • relativePosition is the clicked position on the component.
  • absolutePosition is the absolute clicked position on the menu.
  • action is either 0 for right-click, or 1 for left-click.

render

render(graphics, player) {
}

Called to render this component.

Anonymous components

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

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
    }
});

Clone this wiki locally