Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Sat 09/05/2009 19:30:31

Title: on_key_press [SOLVED]
Post by: Atelier on Sat 09/05/2009 19:30:31
Hullo.

I've been fiddling around trying to make a code to show and hide the icon bar when you press the space key. However, there seems to be a problem and I don't know what it is. I've located this code in the global script.

function on_key_press (eKeyCode keycode) {
  
   if ((keycode == eKeySpace) && gIconbar.Visible = false) {
      gIconbar.Visible = true;
}
  
   if ((keycode == eKeySpace && gIconbar.Visible = true) {
      gIconbar.Visible = false;
}

There. An error message comes up saying "Parse error in expr near 'gIconbar'" I think it may have something to do with brackets. What's wrong with what I've got?

Thanks in advance.
Title: Re: on_key_press
Post by: NsMn on Sat 09/05/2009 19:48:48
You have to write "gIconbar.Visible == false" and "gIconbar.Visible == false". You seem to forgot about it.
Title: Re: on_key_press
Post by: Lt. Smash on Sat 09/05/2009 20:36:47
and you made too much/less brackets.

if ((keycode == eKeySpace) && gIconbar.Visible == false) {
      gIconbar.Visible = true;
}
   
if ((keycode == eKeySpace) && gIconbar.Visible == true) {
      gIconbar.Visible = false;
}
but you can also leave the brackets in the brackets.

and don't forget:
one = sets the value.
== checks if it is the value and returns either true or false.
Title: Re: on_key_press [SOLVED]
Post by: Atelier on Sat 09/05/2009 20:45:09
Thank you very much! (To both of you) It works perfectly and gives just the right effect I was looking for.  :D
Title: Re: on_key_press [SOLVED]
Post by: Khris on Sat 09/05/2009 22:03:28
function on_key_press (eKeyCode keycode) {
 
   if (keycode == eKeySpace) gIconbar.Visible = !gIconbar.Visible;
}
Title: Re: on_key_press [SOLVED]
Post by: densming on Sun 10/05/2009 00:37:52
Actually, I'm not sure why this works.  Since the second if statement doesn't have "else" in front of it, I would think that making the gui visible in the first block would cause the second if condition to be true, which would immediately hide it again.

Does the visible property not actually get set until after the function exits?
Title: Re: on_key_press [SOLVED]
Post by: Atelier on Sun 10/05/2009 10:51:09
Yeah, I had to add an else statement into it afterwards because it would only hide the iconbar not bring it up. But now, it does both. Thanks anyway  :)