on_key_press [SOLVED]

Started by Atelier, Sat 09/05/2009 19:30:31

Previous topic - Next topic

Atelier

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.

NsMn

You have to write "gIconbar.Visible == false" and "gIconbar.Visible == false". You seem to forgot about it.

Lt. Smash

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.

Atelier

#3
Thank you very much! (To both of you) It works perfectly and gives just the right effect I was looking for.  :D

Khris

Code: ags
 function on_key_press (eKeyCode keycode) {
  
   if (keycode == eKeySpace) gIconbar.Visible = !gIconbar.Visible;
}

densming

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?

Atelier

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  :)

SMF spam blocked by CleanTalk