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

Author Topic: I need help for setting pause and play feature in my code!!  (Read 862 times)

0 Members and 1 Guest are viewing this topic.

BOMWE

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
I need help for setting pause and play feature in my code!!
« on: February 20, 2023, 08:49:39 am »
I just looked the methods of implementing the pause and play feature but in mycode when I press the P key nothing happens below is the code of how i did it and also the image of how the code behaves
« Last Edit: February 20, 2023, 10:15:23 am by BOMWE »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10879
    • View Profile
    • development blog
    • Email
Re: I need help for setting pause and play feature in my code!!
« Reply #1 on: February 20, 2023, 09:29:38 am »
Please read and follow the forum rules: https://en.sfml-dev.org/forums/index.php?topic=5559.0

Dumping some code without context or anything isn't useful to anyone.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I need help for setting pause and play feature in my code!!
« Reply #2 on: February 21, 2023, 05:50:13 pm »
The concept of pausing a game (I'll presume game here) is to stop updating the game forward if it's paused. That includes any motion, physics, input (related to the gameplay) and time (don't forget time!). That means any clock should either be paused (if possible), the duration during the paused time removed from future times or stopping feeding the update loop any time (or timestep).

You'll likely need to change 'state' while it's paused. For example, to wait to be unpaused or show a menu. The simplest form would be to track a boolean and do something different depending on its value.

bool isPaused{ false };

while (window.isOpen())
{
    if (!isPaused)
    {
        // .. update gameplay
    }
    else
    {
        // .. wait for unpause
    }
}

The most important thing to remember is that you should not just loop, waiting for input to unpause, but you should be still polling all events and updating the window. You can, of course, use the events differently and/or draw different things to the window when it is paused.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*