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

Author Topic: Menu problem[solved]  (Read 1542 times)

0 Members and 1 Guest are viewing this topic.

Plavsa

  • Newbie
  • *
  • Posts: 2
    • View Profile
Menu problem[solved]
« 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
« Last Edit: July 23, 2013, 12:50:04 pm by Plavsa »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Menu problem
« Reply #1 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Menu problem
« Reply #2 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) are more relevant for your case than real-time inputs.

Plavsa

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Menu problem
« Reply #3 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) are more relevant for your case than real-time inputs.

Thanks, it works. :)
« Last Edit: July 23, 2013, 12:50:36 pm by Plavsa »

 

anything