SFML community forums

Help => General => Topic started by: Plavsa on July 22, 2013, 06:30:31 pm

Title: Menu problem[solved]
Post by: Plavsa on July 22, 2013, 06:30:31 pm
Greetings, fellows I have a bit of a problem here.

Today I decided to get serious with my 2D programming so I started with menu display and have one bug. You see, for now I have 3 buttons, as images which are moving when player press UP or DOWN.But the problem is only first and the last are moving, the one in the middle isnt moving at all or he is moving but too fast for my eyes. Could you tell me how to slow them down? Tried on many things but neither one is helping..


code:

#include <SFML/Graphics.hpp>
#include <sstream>
#include <string>

int meni = 1;

sf::Texture startT;
sf::Texture optionsT;
sf::Texture exitT;

sf::Sprite startS;
sf::Sprite optionsS;
sf::Sprite exitS;

main ()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Pocetak!");

    // pozadina
    sf::Texture pozadinaT;
    pozadinaT.loadFromFile("background.png");

    sf::Sprite pozadinaS;
    pozadinaS.setTexture(pozadinaT);

    // buttons
    startT.loadFromFile("StartButton.png");
    optionsT.loadFromFile("OptionsButton.png");
    exitT.loadFromFile("ExitButton.png");

    startS.setTexture(startT);
    optionsS.setTexture(optionsT);
    exitS.setTexture(exitT);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(meni == 1)
            {
                startS.setPosition(350, 300);
                optionsS.setPosition(300, 350);
                exitS.setPosition(300, 400);
            }
            else if(meni == 2)
            {
                startS.setPosition(300, 300);
                optionsS.setPosition(350, 350);
                exitS.setPosition(300, 400);
            }
            else if(meni == 3)
            {
                startS.setPosition(300, 300);
                optionsS.setPosition(300, 350);
                exitS.setPosition(350, 400);
            }
            if(meni < 1)
                meni = 1;
            if(meni > 3)
                meni = 3;
            meni++;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(meni == 1)
            {
                startS.setPosition(350, 300);
                optionsS.setPosition(300, 350);
                exitS.setPosition(300, 400);
            }
            else if(meni == 2)
            {
                startS.setPosition(300, 300);
                optionsS.setPosition(350, 350);
                exitS.setPosition(300, 400);
            }
            else if(meni == 3)
            {
                startS.setPosition(300, 300);
                optionsS.setPosition(300, 350);
                exitS.setPosition(350, 400);
            }
            if(meni < 1)
                meni = 1;
            if(meni > 3)
                meni = 3;
            meni--;
        }
        window.clear();
        window.draw(pozadinaS);
        window.draw(startS);
        window.draw(optionsS);
        window.draw(exitS);
        window.display();
    }
}
 

Regards
Title: Re: Menu problem
Post by: AlexAUT on July 22, 2013, 07:01:12 pm
But the problem is only first and the last are moving

I can't test it because i'm on my smartphone, but I guess it comes from that your second button has only 2 unique positions. The first and the thrid position are the same (optionsS)



AlexAUT
Title: Re: Menu problem
Post by: G. on July 22, 2013, 07:12:43 pm
It is indeed moving too fast.

You call if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) (and Up) one time per frame. And there are a few hundreds/thousands frame every second, so meni's value is 2 only for one or a few milliseconds.

Maybe events (sf::Event::KeyPressed or Released) (http://www.sfml-dev.org/tutorials/2.0/window-events.php) are more relevant for your case than real-time inputs.
Title: Re: Menu problem
Post by: Plavsa on July 22, 2013, 09:35:24 pm
It is indeed moving too fast.

You call if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) (and Up) one time per frame. And there are a few hundreds/thousands frame every second, so meni's value is 2 only for one or a few milliseconds.

Maybe events (sf::Event::KeyPressed or Released) (http://www.sfml-dev.org/tutorials/2.0/window-events.php) are more relevant for your case than real-time inputs.

Thanks, it works. :)