I have been asked by a few users to make an example of a simple menu
that works with angelscript, that uses the keyboard to move through the
menus, and then does some action when the ENTER key is pressed.
It is currently set up for 3 menus, but can easily be changed to as many
menu sprites as you need. You will need 2 sprites for every menu option.
One for the normal menu texture, and one for highlighted texture.
Enjoy.
that works with angelscript, that uses the keyboard to move through the
menus, and then does some action when the ENTER key is pressed.
It is currently set up for 3 menus, but can easily be changed to as many
menu sprites as you need. You will need 2 sprites for every menu option.
One for the normal menu texture, and one for highlighted texture.
Enjoy.
Code: [Select]
///=========================================================================
/// A Simple menu in AngelScript
///
/// This project requires 6 sprites. One for each of the 3 menus, and one
/// for each of the highlighted menun options. As the currentMenu variable
/// points to a specific menu, a highlighted menu sprite will be displayed
/// instead of the normal menu spite.
///
/// Use the up and down arrows to change the value of the currentMenu
/// variable. 2 Arrays are used to store the OBJ values of the sprites. One
/// array for normal menu sprites, and one array for the highlighted menu
/// sprites.
///
/// For this demo, the normal menu sprites are set to 0.6 opacity, and the
/// highlight menu sprites are left at normal opacity. This is so that you
/// can see which menu item is highlighted when the currentMenu variable
/// focuses on a spexific sprite. In a real game, you will use different
/// textures for the normal and highligted menu sprites, so wont need to set
/// the opacity of the sprites.
///
/// Place the highlight menu sprite and the normal menu sprite for a
/// specific menu in the same location, so that when their visibility is
/// changed, they appear in the same place on the screen
///=========================================================================
///Create an array for the normal menu sprites
int [] MENU_NORMAL (3);
///Create an array for the highlighted menu sprites
int [] MENU_HIGHLIGHT (3);
///Set the maximum number of msnus
int MAX_MENUS = 3;
///Set up a variable to store the current sprite number
///AngelScript counts array entries from 0
int currentMenu =0; //default to top menu = (0 to 2)
///Set up some variables to be used in controlling the
///delay between jumping to the next menu when the up
///down key is held in
int menuTimer = 0;
int menuDelay = 20; ///Numver of frames to delay keys
///
///---This function sets the OBJ values to the MENU arrays----------------
///
void setupMenus()
{
//Store the normal menu sprite OBJ values
MENU_NORMAL[0] = OBJ_66;
MENU_NORMAL[1] = OBJ_0;
MENU_NORMAL[2] = OBJ_110;
//Store the highlight menu sprite OBJ values
MENU_HIGHLIGHT[0] = OBJ_44;
MENU_HIGHLIGHT[1] = OBJ_22;
MENU_HIGHLIGHT[2] = OBJ_132;
}
///
///---Thsi function shows the currently selected menu
///
void showMenu()
{
int i;
for(i=0;i<MAX_MENUS;i++){
if(i != currentMenu){
iObjectHide(MENU_HIGHLIGHT[i]);
}
if(i == currentMenu){
iObjectShow(MENU_HIGHLIGHT[i]);
}
if(i != currentMenu){
iObjectShow(MENU_NORMAL[i]);
}
if(i == currentMenu){
iObjectHide(MENU_NORMAL[i]);
}
}
}
///
///---The Main function--------------------------------------------------
///
void Main()
{
///The initializing block runs only when the scrips is started
if(iInitializing()){
///call a function to store the menu sprite OBJ values
setupMenus();
///Call a function to show the current menu
showMenu();
}
///If the menu timer has not reached the menu delay timer then
///increace the value if the menu timer
if(menuTimer < menuDelay)
menuTimer++;
///Only if the menu timer has run out, then check which key
///the user is pressing, and respond to it. If the timer is
///not ready, then ignore any keys that are pressed.
if(menuTimer == menuDelay){
///If the user presses the UP key, then decrease the value
if (iKeyDown(iKeyCode("DIK_UP"))){
if(currentMenu > 0){
currentMenu--;
///Call a function to show the current menu
showMenu();
///Reset the menu timer, to start the delay count
menuTimer=0;
}
}
///If the user presses the DOWN key, then increase the value
if (iKeyDown(iKeyCode("DIK_DOWN"))){
if(currentMenu < MAX_MENUS-1){
currentMenu++;
///Call a function to show the current menu
showMenu();
///Reset the menu timer, to start the delay count
menuTimer=0;
}
}
///If the ENTER key is pressed then process the current menu
if (iKeyDown(iKeyCode("DIK_RETURN"))){
///Delay with ENTER as well, so you dont start the
///action on every game frame
menuTimer=0;
switch(currentMenu){
case 0:{
//Menu0 - do something
}
case 1:{
//Menu1 - do something
}
case 2:{
//Menu2 - do something
}
}
}
}
}