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

Author Topic: Visual Studio ignores ELSE IF  (Read 4125 times)

0 Members and 2 Guests are viewing this topic.

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Visual Studio ignores ELSE IF
« on: January 07, 2018, 09:16:36 pm »
Hello! So I have this most simple project in which I have 2 events: when "W" is pressed and when "S". As you can see there are 3 lines for where must be square after pressing buttons. BUT! It works only like total fall to the 3nd line or total up to the 1nd line AND(!) if you press buttons quickly you can see that 2nd line actually is present but code skips it. I can't understand why, please help. Maybe my VS is broken?

UPDATE* He-he I forgot to add a project ;D
https://www.dropbox.com/s/7cia15c6lto9600/Test.rar?dl=0
« Last Edit: January 07, 2018, 09:18:41 pm by Hastingsin »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #1 on: January 07, 2018, 10:45:00 pm »
Please don't post a link to an archive, put the relevant code directly here.
Laurent Gomila - SFML developer

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #2 on: January 07, 2018, 11:28:07 pm »
#include <SFML/Graphics.hpp>

using namespace sf;
using namespace std;

String CurrentlyAt = "Menu";
int Lal = 1;


int main()
{
        RenderWindow window(VideoMode(1244, 772), "Test");
       
        Texture TextureMenu;
        TextureMenu.loadFromFile("Menu.png");
        Sprite Menu(TextureMenu);

        Texture TextureOptionsInMenu;
        TextureOptionsInMenu.loadFromFile("1.png");
        Sprite OptionsInMenu(TextureOptionsInMenu);

        OptionsInMenu.setPosition(487, 293);

        while (window.isOpen())
        {
                // Process events
                Event event;
                while (window.pollEvent(event))
                {
                        // Close window: exit
                        if (event.type == Event::Closed)
                                window.close();
                }
                if (Keyboard::isKeyPressed(Keyboard::Key::W))
                {
                        if (CurrentlyAt == "Menu")
                        {
                                if (Lal == 2)
                                {
                                        OptionsInMenu.setPosition(487, 293);
                                        Lal = 1;
                                }
                                else if (Lal == 1)
                                {
                                        OptionsInMenu.setPosition(487, 236);
                                        Lal = 0;
                                }
                        }
                }
                if (Keyboard::isKeyPressed(Keyboard::Key::S))
                {
                        if (CurrentlyAt == "Menu")
                        {
                                if (Lal == 0)
                                {
                                        OptionsInMenu.setPosition(487, 293);
                                        Lal = 1;
                                }
                                else if (Lal == 1)
                                {
                                        OptionsInMenu.setPosition(487, 348);
                                        Lal = 2;
                                }
                        }
                }

                window.clear();
                window.draw(Menu);
                window.draw(OptionsInMenu);
                window.display();
        }
        return 0;
}
« Last Edit: January 08, 2018, 06:36:09 am by Laurent »

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #3 on: January 07, 2018, 11:32:08 pm »
Please don't post a link to an archive, put the relevant code directly here.
So I added a code but I do not think that there is any problem with it. It just goes through both "if" when you press "W" or "S". Btw, if you make breakpoint it will crush cus even VS understand that if you chose one of the IFs you can't go to others.
Such a lol.
Try this code please anyway. I still think that my VS is broken.

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Visual Studio ignores ELSE IF
« Reply #4 on: January 07, 2018, 11:54:24 pm »
Code works as expected on my end. What do you want the code to do?

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #5 on: January 08, 2018, 12:11:51 am »
Code works as expected on my end. What do you want the code to do?

Start program, the square must be on the 2 line. Press W, the square must be on the 1 line. Then press S, the square must be in the 2 line again BUT in MY VS it goes to 3 line. And then I press W again it goes to 1 line. and etc. etc. etc. 2 line is never achieved. Soooo. Try again. And I will grateful for screenshots.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #6 on: January 08, 2018, 11:32:59 am »
Your game loop will go as fast as possible or if VSync is enforced at 60fps, that is around 16.66ms. Now if you hold your key down for longer than 16.66ms (which you most likely do) the loop will execute at least twice and the the variable will be set.

If you want to "react" to key presses, it's recommended to use events instead.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #7 on: January 08, 2018, 11:53:48 am »
Your game loop will go as fast as possible or if VSync is enforced at 60fps, that is around 16.66ms. Now if you hold your key down for longer than 16.66ms (which you most likely do) the loop will execute at least twice and the the variable will be set.

If you want to "react" to key presses, it's recommended to use events instead.

Oh so it is all about time. Can you please provide me the code with events for this project? I want my buttons to trigger only once when it is pressed.
Thx for the answer!!!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #8 on: January 08, 2018, 11:56:36 am »
See the official tutorial and documentation.

https://www.sfml-dev.org/learn.php
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #9 on: January 08, 2018, 12:05:56 pm »
See the official tutorial and documentation.

https://www.sfml-dev.org/learn.php

I did it, thx!
Here is what i got in the end.
Code: [Select]
#include <SFML/Graphics.hpp>

using namespace sf;
using namespace std;

String CurrentlyAt = "Menu";
int Lal = 1;


int main()
{
RenderWindow window(VideoMode(1244, 772), "Test");

Texture TextureMenu;
TextureMenu.loadFromFile("Menu.png");
Sprite Menu(TextureMenu);

Texture TextureOptionsInMenu;
TextureOptionsInMenu.loadFromFile("1.png");
Sprite OptionsInMenu(TextureOptionsInMenu);

OptionsInMenu.setPosition(487, 293);

while (window.isOpen())
{
window.clear();
// Process events
Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
{
if (Keyboard::isKeyPressed(Keyboard::Key::W))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 2)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 236);
Lal = 0;
}
}
}
if (Keyboard::isKeyPressed(Keyboard::Key::S))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 0)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 348);
Lal = 2;
}
}
}
}
}


window.draw(Menu);
window.draw(OptionsInMenu);
window.display();
}
return 0;
}
« Last Edit: January 08, 2018, 01:13:22 pm by Hastingsin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #10 on: January 08, 2018, 12:25:55 pm »
That is wrong, check the event tutorial again.

Also use [code=cpp][/code] tag when posting code to the forum.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #11 on: January 08, 2018, 01:22:01 pm »
That is wrong, check the event tutorial again.

Also use [code=cpp][/code] tag when posting code to the forum.

#include <SFML/Graphics.hpp>

using namespace sf;
using namespace std;

String CurrentlyAt = "Menu";
int Lal = 1;


int main()
{
        RenderWindow window(VideoMode(1244, 772), "Test");
       
        Texture TextureMenu;
        TextureMenu.loadFromFile("Menu.png");
        Sprite Menu(TextureMenu);

        Texture TextureOptionsInMenu;
        TextureOptionsInMenu.loadFromFile("1.png");
        Sprite OptionsInMenu(TextureOptionsInMenu);

        OptionsInMenu.setPosition(487, 293);
        window.setKeyRepeatEnabled(false);
        while (window.isOpen())
        {
                window.clear();
                // Process events
                Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                // window closed
                        case sf::Event::Closed:
                                window.close();
                                break;

                                // key pressed
                        case sf::Event::KeyPressed:
                                if (Keyboard::isKeyPressed(Keyboard::Key::W))
                                {
                                        if (CurrentlyAt == "Menu")
                                        {
                                                if (Lal == 2)
                                                {
                                                        OptionsInMenu.setPosition(487, 293);
                                                        Lal = 1;
                                                }
                                                else if (Lal == 1)
                                                {
                                                        OptionsInMenu.setPosition(487, 236);
                                                        Lal = 0;
                                                }
                                        }
                                }
                                if (Keyboard::isKeyPressed(Keyboard::Key::S))
                                {
                                        if (CurrentlyAt == "Menu")
                                        {
                                                if (Lal == 0)
                                                {
                                                        OptionsInMenu.setPosition(487, 293);
                                                        Lal = 1;
                                                }
                                                else if (Lal == 1)
                                                {
                                                        OptionsInMenu.setPosition(487, 348);
                                                        Lal = 2;
                                                }
                                        }
                                }
                                break;

                                // we don't process other types of events
                        default:
                                break;
                        }
                }
               

                window.draw(Menu);
                window.draw(OptionsInMenu);
                window.display();
        }
        return 0;
}
 


Thx for spending your time on me ;D
I hope this is the right one.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #12 on: January 08, 2018, 02:02:10 pm »
Whether you use if/else or switch is not really important, what is important, is that you need to check the event's key and not use isKeyPressed. Again read the tutorial on events, especially the big red box! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hastingsin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Visual Studio ignores ELSE IF
« Reply #13 on: January 08, 2018, 02:20:49 pm »
Whether you use if/else or switch is not really important, what is important, is that you need to check the event's key and not use isKeyPressed. Again read the tutorial on events, especially the big red box! ;)

I'm so thankful to you for spending your time on me ;D

Soo this is the very last solution that I found.
#include <SFML/Graphics.hpp>

using namespace sf;
using namespace std;

String CurrentlyAt = "Menu";
int Lal = 1;


int main()
{
        RenderWindow window(VideoMode(1244, 772), "Test");
       
        Texture TextureMenu;
        TextureMenu.loadFromFile("Menu.png");
        Sprite Menu(TextureMenu);

        Texture TextureOptionsInMenu;
        TextureOptionsInMenu.loadFromFile("1.png");
        Sprite OptionsInMenu(TextureOptionsInMenu);

        OptionsInMenu.setPosition(487, 293);
        window.setKeyRepeatEnabled(false);
        while (window.isOpen())
        {
                window.clear();
                // Process events
                Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                // window closed
                        case sf::Event::Closed:
                                window.close();
                                break;

                                // key pressed
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::W)
                                {
                                        if (CurrentlyAt == "Menu")
                                        {
                                                if (Lal == 2)
                                                {
                                                        OptionsInMenu.setPosition(487, 293);
                                                        Lal = 1;
                                                }
                                                else if (Lal == 1)
                                                {
                                                        OptionsInMenu.setPosition(487, 236);
                                                        Lal = 0;
                                                }
                                        }
                                }
                                else if (event.key.code == sf::Keyboard::S)
                                {
                                        if (CurrentlyAt == "Menu")
                                        {
                                                if (Lal == 0)
                                                {
                                                        OptionsInMenu.setPosition(487, 293);
                                                        Lal = 1;
                                                }
                                                else if (Lal == 1)
                                                {
                                                        OptionsInMenu.setPosition(487, 348);
                                                        Lal = 2;
                                                }
                                        }
                                }
                                break;

                                // we don't process other types of events
                        default:
                                break;
                        }
                }
               

                window.draw(Menu);
                window.draw(OptionsInMenu);
                window.display();
        }
        return 0;
}

 

anything