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

Author Topic: Pixels counting  (Read 1366 times)

0 Members and 1 Guest are viewing this topic.

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
Pixels counting
« on: September 08, 2017, 07:56:09 pm »
Hello. I make a button:
Quote
         sf::RectangleShape button1(sf::Vector2f(250, 70));
         button1.setPosition(sf::Vector2f(350, 200));
And now i need do somethink if player pressed on this button:
Quote
         if (menu == 0) {
            if (sf::Mouse::getPosition().x > 250 && sf::Mouse::getPosition().x < 600 &&
               sf::Mouse::getPosition().y > 200 && sf::Mouse::getPosition().y < 270) {
               if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) menu = 1;
            }
but menu = 1 if i pressed in another place (not on button1). Can someone explain me why?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Pixels counting
« Reply #1 on: September 08, 2017, 08:39:00 pm »
Can you show a self contained main.cpp example with that problem?

Also, to get position of mouse within a window you should pass windows as argument, with no arguments it's relative to desktop:
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Mouse.php

And to then convert int pixel into float coords you should use mapPixelToCoords method in RenderWindow.
Back to C++ gamedev with SFML in May 2023

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Pixels counting
« Reply #2 on: September 08, 2017, 08:46:53 pm »
Thanks, my problem was that I not insert window like argument :D