Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Menu-Bug invloving sf::Event::keyPressed  (Read 2468 times)

0 Members and 1 Guest are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Menu-Bug invloving sf::Event::keyPressed
« on: March 25, 2019, 11:51:33 pm »
if (this->mainMenuActive)
{
   mainMenuEvents();
}

if (this->startMenuActive)
{
   this->startMenuEvents();
}

if (this->resourceMenuActive)
{
   this->resourceMenuEvents();
}

void Game::mainMenuEvents()
{

   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->mainMenuHighlight;
         if (this->mainMenuHighlight > MAIN_COMMAND)
         {
            this->mainMenuHighlight = MAIN_RESOURCE;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->mainMenuHighlight;
         if (this->mainMenuHighlight < MAIN_RESOURCE)
         {
            this->mainMenuHighlight = MAIN_COMMAND;
         }

      }break;


      case sf::Keyboard::Enter:
      {
         this->mainMenuActive = false;

         if (this->mainMenuHighlight == MAIN_RESOURCE)
         {            
            this->resourceMenuActive = true;
            std::cout << "Resource: " << resourceMenuActive << std::endl;
         }         

      }break;

      case sf::Keyboard::Escape:
      {

         this->mainMenuActive = false;
         this->startMenuActive = true;

      }break;
   }
}


void Game::startMenuEvents()
{
   switch (this->events.key.code)
   {
   case sf::Keyboard::Down:
   {
      ++this->startMenuHighlight;
      if (this->startMenuHighlight > START_EXIT_GAME)
      {
         this->startMenuHighlight = START_NEW_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Up:
   {
      --this->startMenuHighlight;
      if (this->startMenuHighlight < START_NEW_GAME)
      {
         this->startMenuHighlight = START_EXIT_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Enter:
   {
      if (this->startMenuHighlight == START_NEW_GAME)
      {
         this->startMenuActive = false;
         this->mainMenuActive = true;
      }

      if (this->startMenuHighlight == START_EXIT_GAME)
      {
         this->gameIsRunning = false;
      }
   }

   }
}


void Game::resourceMenuEvents()
{
   
   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->resourceMenuHighlight;
         if (this->resourceMenuHighlight > CHEMIST)
         {
            this->resourceMenuHighlight = BANKMAN;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->resourceMenuHighlight;
         if (this->resourceMenuHighlight < BANKMAN)
         {
            this->resourceMenuHighlight = CHEMIST;
         }

      }break;

      case sf::Keyboard::Enter:
      {
         if (resourceMenuHighlight == BANKMAN)
         {
            ++this->overviewInfoValue;
         }

      } break;

      case sf::Keyboard::Escape:
      {
         this->resourceMenuActive = false;
         this->mainMenuActive = true;
      }break;
   }
}


----(THIS IS JUST A LITTLE BIT OF CODE FROM THE PROGRAM)----

My program renders 3 menus  at the moment: startMenu -> mainMenu -> resourceMenu.

all menus handle sf::Event::KeyPressed: (Enter/Up/Down).

in resourceMenu when i press enter a number gets updated on the screen.

THE PROBLEM is that when i press enter from the mainMenu-Screen, the resourceMenu also registers that Enter was pressed and thus the resource number gets updated as soon as the resourceMenu is rendered to the screen.



What i want to achieve is this:

mainMenu (if Enter is pressed) -> resourceMenu displayed (if Enter is pressed) -> update resourceValue.



what i have:

mainMenu (if Enter is pressed) -> resourceMenu displayed (and since you pressed Enter in mainMenu resourceValue will be updated from that keyPress)


i guess i want to separate the sf::keyboard::Enter from each menu and make it so that the menus don't respond to eachothers keypress-Events.



Thanks in advance!



G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Menu-Bug invloving sf::Event::keyPressed
« Reply #1 on: March 26, 2019, 01:17:29 am »
You can put your if inside else if you don't want to have multiple menus active during the same frame.
if (this->mainMenuActive)
{
   mainMenuEvents();
}
else if (this->startMenuActive)
{
   this->startMenuEvents();
}
else if (this->resourceMenuActive)
{
   this->resourceMenuEvents();
}

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Menu-Bug invloving sf::Event::keyPressed
« Reply #2 on: March 26, 2019, 07:42:25 am »
Oh thanks, it worked! :D