Script:System/menu.js

From Spherical
Jump to: navigation, search

The system script menu.js provides a simple menu object.

Usage


Variables and functions of menu.js

  • object constructor Menu()
    • properties of objects of Menu type:
    • methods of objects of Menu type:
      • addItem(name, callback [, color]) - adds an entry to the menu. The string 'name' specifies the menu text, 'callback' is a function to be called when the item is selected and 'color' is a color object defining the color of the item. If the color remains unspecified it is set to white.
      • execute(x, y, width, height) - this displays and executes the menu. The arguments 'x', 'y', 'width' and 'height' define the size of the menu window. If the menu items don't fit into the window, the menu will be scrollable.

Example

RequireSystemScript("menu.js");

var MainMenu = new Menu();
MainMenu.addItem("Items", ItemMenu);
MainMenu.addItem("Equip", EquipMenu);
MainMenu.addItem("Magic", MagicMenu);
MainMenu.addItem("Stats", StatsMenu);
MainMenu.addItem("Quit", Exit);

MainMenu.execute(20, 20, 50, 60);

The following functions are called if their respective menu item is selected. They have to be defined for the above to work. Exit(), which is called by the last menu item, doesn't need to be defined, because it is a function of the Sphere API.

function ItemMenu() {
  //...
}

function EquipMenu() {
  //...
}

function MagicMenu() {
  //...
}
 
function StatsMenu() {
  //...
}

Notes

  • The menu arrow is moved using the up and down directional keys. A selection is always confirmed with enter. Joystick input is not supported.
  • If necessary the up and down arrows will be displayed near the right border of the menu window.
  • To specify a function as an escape_function or callback function you may not use brackets.
    /* This won't work:     */
    MyMenu.escape_function = DoSomething();
    MyMenu.addItem("New Game", NewGame());
    /* This will work fine: */
    MyMenu.escape_function = DoSomething;
    MyMenu.addItem("New Game", NewGame);
  • Menu.js automatically includes the system script colors.js.
  • A tutorial can be found in the docs/system_scripts/ directory of Sphere.
  • The famous flik_menu.js is not a system script, but works similar to menu.js. It allows you to do much more though.

External links