title: (C++) Executing an instruction inside a game loop once (SFML)
body: <p><strong>The answer to this question came to me halfway through writing it and honestly I feel like I should finish what I started and document it since I've been stuck with this bug the whole day and it might come in handy later.</strong></p>
  <p>I'm using SFML and I have a number of buttons, and I want to play a blip sound-effect when the mouse cursor hovers over one of them...</p>
  <p>This is the method inside the button class that tests for collision between the mouse cursor and a button:</p>
  <pre><code>bool menu_button::button_collision() {
  
      if ((cursor.getPosition().x &lt;= (text_box.getPosition().x)+text_box.getSize().x/2)
          &amp;&amp; (cursor.getPosition().x &gt;= (text_box.getPosition().x) - text_box.getSize().x / 2)
          &amp;&amp;(cursor.getPosition().y &lt;= (text_box.getPosition().y) + text_box.getSize().y / 2)
          &amp;&amp; (cursor.getPosition().y &gt;= (text_box.getPosition().y) - text_box.getSize().y / 2)) {
          
          text.setFillColor(sf::Color(sf::Color::Yellow));
          coll = true;
      }
      else { text.setFillColor(sf::Color(sf::Color::White)); coll = false;  }
      return coll;
  }
  </code></pre>
  <p>And this is the code that plays the sound effect when the cursor hovers over a button:</p>
  <pre><code>for (int j = init; j &lt; limit; j++) { //This is a loop that tests every button.
          collision = buttons[j].button_collision();// 'collision' is a boolean that stores the value returned by the collision function.
  
          if (collision == true) {
  
                  m_hover.play();
  
              }
  //Other instructions go here...
  }
  </code></pre>
  <p>Now this code works just fine, but since it's inside the game loop, which repeats every frame, the sound is going to play an 'x' amount of times per second ('x' being the number of frames per second) which is not what I wanted, so I modified the code to look like this:</p>
  <pre><code>for (int j = init; j &lt; limit; j++) {
          collision = buttons[j].button_collision();
  
          if (collision == true) {
  
              if (collision2 == false) { // 'collision2' is another boolean, that becomes true AFTER the sound-effect have already played.
  
                  m_hover.play();
                  collision2 = true;
  
              }
  
          }
  //Other instructions go here...
  }
  </code></pre>
  <p>Now, the sound-effect plays the first time the cursor hovers over a button, but but ONLY the first time, and then it goes completely silent, so clearly we have another problem on our hands...</p>
  <p>So I added this line under the parent condition (The one that tests <code>collision</code>):</p>
  <pre><code>else { collision2 = false; }
  </code></pre>
  <p>I thought this would work, but now I was back to the first problem, where the sound-effect would be playing constantly for every frame in which the cursor was hovering over a button...</p>
  <p>After a bit of tinkering, I came to realize that <code>collision</code> resets to <code>false</code> every frame, which is the reason the sound-effect ends up playing every frame, but I wasn't sure why and where/when this was happening, or how to get around it.</p>