IsKeyPressed pissing me off-fa-fa [solved]

Started by DrewCCU, Sun 20/06/2010 21:45:12

Previous topic - Next topic

DrewCCU

Okay - i know there are like a billion threads similar to this one and i've done a quick search and actually found one that was very similar to what i need.  However, playing with that solution only got me so far.

I want to use the "Tab" key as a "toggle" switch.  When you press the Tab key your mouse is restricted to a certain area of the screen and moving the mouse to the edge of the boundary moves the character in that direction.  ANd when you press the tab key again - you go back to being able to move your mouse wherever you want to.  Well i have everything coded how i think it should work and it doesn't.  When testing, pressing the tab key does nothing.  

Here is my code:

please note that the following script is in the RepExec part of the script and "pressingTAB" is a bool that is defined at the top of the room script.

Code: ags

  if ((IsKeyPressed(9)) && (!pressingTAB)) {
    pressingTAB = true;
  }
  if ((IsKeyPressed(9)) && (pressingTAB)) {
    pressingTAB = false;
  }
  if (pressingTAB) {
      //Cursor fixed to viewscreen
      mouse.SetBounds(208, 130, 588, 355);
      //Mouse Steer
      if (mouse.x <= 221) cEgo.x = cEgo.x - 6;
      if (mouse.x >= 575) cEgo.x = cEgo.x + 6;
      if (mouse.y <= 143) cEgo.y = cEgo.y - 6;
      if (mouse.y >= 342) cEgo.y = cEgo.y + 6;
        }
  else if (!pressingTAB) {
    mouse.SetBounds(0, 0, 0, 0);
  }


So the above code doesn't work like i want it to.  However, when i was testing i added a couple lines to the code (see below) and it worked.  But i need it to work without those lines.

Code: ags

if ((IsKeyPressed(9)) && (!pressingTAB)) {
    pressingTAB = true;
    Display("PressingTAB is true.");
  }
  if ((IsKeyPressed(9)) && (pressingTAB)) {
    pressingTAB = false;
    Display("PressingTAB is false.");
  }
  if (pressingTAB) {
      //Cursor fixed to viewscreen
      mouse.SetBounds(208, 130, 588, 355);
      //Mouse Steer
      if (mouse.x <= 221) cEgo.x = cEgo.x - 6;
      if (mouse.x >= 575) cEgo.x = cEgo.x + 6;
      if (mouse.y <= 143) cEgo.y = cEgo.y - 6;
      if (mouse.y >= 342) cEgo.y = cEgo.y + 6;
        }
  else if (!pressingTAB) {
    mouse.SetBounds(0, 0, 0, 0);
  }
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Crimson Wizard

#1
Before even checking your code posted above, I want to ask, why do you use "IsKeyPressed" function at all if TAB toggles some mode on and off? For such behavior it is much better to catch key presses in on_key_press function.

EDIT: As for the code - yes, I can see mistakes there, but first I'll wait for your answer, because it may appear that this code is not needed at all :)

DrewCCU

Quote from: Crimson Wizard on Sun 20/06/2010 21:52:08
Before even checking your code posted above, I want to ask, why do you use "IsKeyPressed" function at all if TAB toggles some mode on and off? For such behavior it is much better to catch key presses in on_key_press function.

EDIT: As for the code - yes, I can see mistakes there, but first I'll wait for your answer, because it may appear that this code is not needed at all :)

I tried it with on_key_press too and it didn't work... here is my code for that:

At the top of the room script:
Code: ags

function on_key_press(eKeyCode keycode){
  if ((keycode == eKeyTab) && (!pressingTAB)) {
    pressingTAB = true;
  }
  if ((keycode == eKeyTab) && (pressingTAB)) {
    pressingTAB = false;
  }
}


In RepExec:
Code: ags

if (pressingTAB) {
      //Cursor fixed to viewscreen
      mouse.SetBounds(208, 130, 588, 355);
      //Mouse Steer
      if (mouse.x <= 221) cEgo.x = cEgo.x - 6;
      if (mouse.x >= 575) cEgo.x = cEgo.x + 6;
      if (mouse.y <= 143) cEgo.y = cEgo.y - 6;
      if (mouse.y >= 342) cEgo.y = cEgo.y + 6;
        }
  else if (!pressingTAB) {
    mouse.SetBounds(0, 0, 0, 0);
  }
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Crimson Wizard

Hmm, I want to make this clear once again: do you need TAB be pressed all the time for mouse cursor to be limited to certain area, or do you need it be pressed once to set this behavior on, and then later another time to set it off?
I ask again, because your boolean value is called "pressingTAB", which sounds like it meant be pressed continiously.

Anyway, regarding the code and assuming TAB toggles the mode on/off.

First of all, you are making classic mistake there, by not using wonderful NOT operator :)

Here how it should look like:
Code: ags

function on_key_press(eKeyCode keycode){

  if (keycode == eKeyTab) pressingTAB = !pressingTAB;

}


This will simply "invert" boolean variable to opposite value.

Your second mistake is that you set mouse bounds every tick in rep_exec. You shouldn't do it. Instead, put that in the key_press as well, like:
Code: ags

function on_key_press(eKeyCode keycode){

  if (keycode == eKeyTab) pressingTAB = !pressingTAB;
  if (pressingTAB) {
      //Cursor fixed to viewscreen
      mouse.SetBounds(208, 130, 588, 355);
        }
  else
    mouse.SetBounds(0, 0, 0, 0);
  }

}


What should be left in rep_exec is perhaps this:
Code: ags

if (pressingTAB) {
      //Mouse Steer
      if (mouse.x <= 221) cEgo.x = cEgo.x - 6;
      if (mouse.x >= 575) cEgo.x = cEgo.x + 6;
      if (mouse.y <= 143) cEgo.y = cEgo.y - 6;
      if (mouse.y >= 342) cEgo.y = cEgo.y + 6;
        }
  }


Now... I honestly can't see what can not work here. If it still doesn't work as expected, maybe you can give more details about what happens?

Calin Leafshade

you need an else if.

using two ifs just causes the code to turn it on and then turn it off again.
or more simply:

Code: ags

function on_key_press(eKeyCode keycode){
  if (keycode == eKeyTab) pressingTAB = !pressingTAB;
}


EDIT: beaten by Ninja Wizard

DrewCCU

Quote from: Calin Leafshade on Sun 20/06/2010 22:14:51
you need an else if.

using two ifs just causes the code to turn it on and then turn it off again.
or more simply:

Code: ags

function on_key_press(eKeyCode keycode){
  if (keycode == eKeyTab) pressingTAB = !pressingTAB;
}


Ah, thanks.  Adding the word "Else" made all the difference. lol thanks.
EDIT: beaten by Ninja Wizard
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

SMF spam blocked by CleanTalk