Keycode!= being triggered [SOLVED]

Started by Kinoko, Mon 22/08/2005 05:42:25

Previous topic - Next topic

Kinoko

This probably isnt going to do much good since I cant post my code here (no net at home yet, and the laptop at work doesnt take my USB drive) but Ill try to give you the gist of my problem anyway.

I have a little mini game involving sequences of objects turning on and off in a paticular room.  Its all triggered with global ints.

Basically, I start a conversation with a character which increases glob. int 25 to say, 3. Its now up to the player to press the right keys in the right order (no time limit involved) so I have a line to the effect of

if ((GetGlobalInt(25)==3) && (keycode==keyx)) MusicNote; // This triggers an increase in the global int among other things
else if ((GetGlobalInt(25)==3) && (keycode!=keyx)) EndMusic; // This basically causes you to lose the mini-game, resetting everything.
if ((GetGlobalInt(25)==4) && (keycode==keyz)) MusicNote;
else if ((GetGlobalInt(25)==4) && (keycode!=keyz)) EndMusic;

Thats roughly it (from memory) and the same code is repeated several times for all the different `levels` of GlobalInt 25.

I hope you can understand my meaning here. Again, sorry I cant post the proper code).

Now, it all works fine except when I execute this, once the situation arises to push the correct key, I press it, MusicNote happens, the GlobalInt goes up and pretty much straight away EndMusic happens.

What I was -hoping- is that you press the correct key, MusicNote happens sending the GI value up, then you press the second correct key, etc etc... I cant figure out why EndMusic is being triggered.

I checked the code from top to bottom and its all working perfectly. I can take the first EndMusic out and then the mini-game doesn:t end until it hits the next line of EndMusic. For some reason, the ` != ` line for the increased GI value is being triggered right after the ` == ` line. I thought maybe having no key pressed was triggering the ` != key ` line, so I added a ` && (keycode!=0) ` line but it made no difference.

Does anyone understand the way keycode works well enough to see whats going wrong here? Im sure its just something to do with the way keycode is triggered/checked.

Kweepa

Quote from: Kinoko on Mon 22/08/2005 05:42:25
if ((GetGlobalInt(25)==3) && (keycode==keyx)) MusicNote; // This triggers an increase in the global int among other things
else if ((GetGlobalInt(25)==3) && (keycode!=keyx)) EndMusic; // This basically causes you to lose the mini-game, resetting everything.
else
if ((GetGlobalInt(25)==4) && (keycode==keyz)) MusicNote;
else if ((GetGlobalInt(25)==4) && (keycode!=keyz)) EndMusic;

If you don't add the else, then the global int is increased in the first 'if' and immediately checked in the second.

I'd do it like this to clean things up a bit:

Code: ags

int musicKeyForStage[6];
int musicSoundForStage[6];
int musicStage;
int musicEndStage;
bool playingMusicGame = false;

function SetupTune1()
{
  musicKeyForStage[0] = 'X';
  musicSoundForStage[0] = 1;
  musicKeyForStage[1] = 'Z';
  musicSoundForStage[0] = 2;
  ...
  musicStage = 0;
  musicEndStage = 4;  // for example
  playingMusicGame = true;
}

function MusicNote()
{
  PlaySound(musicSoundForStage[musicStage]);
  musicStage++;
  if (musicStage == musicEndStage)
  {
    playingMusicGame = false;
    // hooray!
  }
}

function EndMusic()
{
  playingMusicGame = false;
  // boo!
}

function on_key_pressed(int keycode)
{
  if (playingMusicGame)
  {
    if (keycode == musicKeyForStage[musicStage])
    {
      MusicNote();
    }
    else
    {
      EndMusic();
    }
  }
}
Still waiting for Purity of the Surf II

Kinoko

#2
Ah, of course! Thanks Steve. I cant check this until I get home today but that`s surely the problem. I`m usually so careful with my elses too...

EDIT: Yep, that was the problem! Thanks :)

SMF spam blocked by CleanTalk