Help with keyboard-based menu [SOLVED]

Started by thecatamites, Sun 05/07/2009 00:57:29

Previous topic - Next topic

thecatamites

Hello,
I'm trying to set up a basic main menu controlled by keyboard presses: the 'new game', 'load game', 'quit' options are arranged vertically and selected by the up and down arrows. The way I've tried to do it is with three 'arrow' objects, one next to each option, whose visibility is controlled by an on_key_press function in the room script. The problem is that I can't get the script to work, and my three attempts to rewrite it have resulted in the exact same problem: the first arrow moves to the second when the 'up' key is pressed and does nothing when the 'down' key is pressed; the second arrow goes back to the first when either the 'up' or 'down' arrows are pressed; the third arrow remains invisible at all times. I'm sure there's something simple I'm missing but I can't seem to get it!

Here's the script (I think I'm using AGS 3.1). The first 'arrow' object is set to visible in the main room editor, the other two are set to invisible, but fiddling with this does nothing to help. '372' is the keycode for the 'up' arrow, '380' the keycode for the 'down' one, the first, second and third objects are represented as object[0], object[1] and object[2] respectively.

function on_key_press(int keycode)
{
if (keycode == 372)
{
 if (object[0].Visible == true)
 {
   object[0].Visible = false;
   object[2].Visible = true;
 }
 if (object[1].Visible == true)
 {
   object[1].Visible = false;
   object[0].Visible = true;
 }
 if (object[2].Visible == true)
 {
   object[2].Visible = false;
   object[1].Visible = true;
   }
}
if (keycode == 380)
{
 if (object[0].Visible == true)
 {
   object[0].Visible = false;
   object[1].Visible = true;
 }
 if (object[1].Visible == true)
 {
   object[1].Visible = false;
   object[2].Visible = true;
 }
 if (object[2].Visible == true)
 {
   object[2].Visible = false;
   object[0].Visible = true;
   }
 }
}
Games 4 Schools Dot Com play games made for schools and teens to lkearn

Matti

#1
You forgot one important word: ELSE.


Code: ags

function on_key_press(int keycode)
{
if (keycode == 372)
{
  if (object[0].Visible == true)
  {
    object[0].Visible = false;
    object[2].Visible = true;
  }
  else if (object[1].Visible == true)  // here
  {
    object[1].Visible = false;
    object[0].Visible = true;
  }
  else if (object[2].Visible == true)  // here
  {
    object[2].Visible = false;
    object[1].Visible = true;
    }
}
if (keycode == 380)
{
  if (object[0].Visible == true)
  {
    object[0].Visible = false;
    object[1].Visible = true;
  }
  else if (object[1].Visible == true)   // here
  {
    object[1].Visible = false;
    object[2].Visible = true;
  }
  else if (object[2].Visible == true)   // here
  {
    object[2].Visible = false;
    object[0].Visible = true;
    }
  }
}


Oh, and please use the code tags, it's much easier to read then..

Ghost

#2
A minor point, but instead of fiddling around with three objects, you can also use only one, and change its position.
(In the following code the arrow object is oArrow)

Code: ags

int position = 0;
if (position == 0)
{
  oArrow.Y = 100;
}
else if {position == 1)
{
  oArrow.Y = 200;
}
else if { position == 2)
{
  oArrow.Y = 300;
}


And for the keypresses:

Code: ags

if (keycode == 371)
{
  if (position > 0)
  {
    position--;
  }
  else
  {
  position = 2;
  }
}

if (keycode == 380)
{
  if (position < 2)
  {
    position++;
  }
  else
  {
  position = 0;
  }
}


While your code works (thanks to Matti's correction), I often find the Global Script is easily cluttered up by long (and mostly repetitive) code ;) Also makes it way easier to add more entries to the menu should you need to.

thecatamites

I added the 'else' and it works perfectly now, thank you! And Ghost, I'll keep that in mind for when I start trying to make the GUI :)
Games 4 Schools Dot Com play games made for schools and teens to lkearn

SMF spam blocked by CleanTalk