G
G
Gotenks2019-01-15 21:02:47
C++ / C#
Gotenks, 2019-01-15 21:02:47

I don't understand what is the error in the code?

The code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
      sf::Vector2i m;
  
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "ПИРИВАДЧИК!!!!");
    sf::Texture OneWindow;
    OneWindow.loadFromFile("OneWindow.png");
    sf::Sprite OneWindowS;
    OneWindowS.setTexture(OneWindow);
    OneWindowS.setPosition(0, 0);

    sf::Texture knopka1;
    knopka1.loadFromFile("Knopka1.png");
    sf::Sprite knopka1S;
    knopka1S.setTexture(knopka1);
    knopka1S.setPosition(75, 541);
    
    sf::Texture knopka2;
    knopka1.loadFromFile("Knopka2.png");
    sf::Sprite knopka2S;
    knopka2S.setTexture(knopka2);
    knopka2S.setPosition(75, 141);

    bool checkPos1 = false;
    bool checkPos2 = false;
  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      switch (event.type)
      {
      case sf::Event::Closed:
        window.close();
        break;
      case sf::Event::KeyPressed:
        if (event.key.code == sf::Keyboard::Escape)
         	window.close();
        break;
      case sf::Event::MouseMoved:
        checkPos1 = knopka1S.getGlobalBounds().contains(event.mouseMove.x, event.mouseMove.y);
        checkPos2 = knopka2S.getGlobalBounds().contains(event.mouseMove.x, event.mouseMove.y);
        break;
      }
    }

    window.clear();
    window.draw(OneWindowS);
    if (checkPos1)
      window.draw(knopka1S);
    if (checkPos2)
        window.draw(knopka2S);
    window.display();
  }
  return 0;
}
1 screen - the original window of the program
SqTrZsE.jpg
2 screen - hovered the mouse over the button below
5pxE69u.jpg

Problem: I move the mouse cursor over the top button, nothing happens, but according to the idea - it should.
Where is the mistake? Please help me understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-01-16
@Gotenks

Have you tried debugging your code?
Put a break point on the logic that should be called. See if it triggers. If not, figure out why. If yes, then see why the logic does not work. Well, debug the program step by step.
This is not some general question you have. This is a special case that just needs to be debugged. Not sure if anyone wants to do this for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question