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

Author Topic: How to detect if mouse clicked inside a certain box or rectangle  (Read 10447 times)

0 Members and 1 Guest are viewing this topic.

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
By box or rectangle I do not refer to the rect drawn by me. I want to know if a user clicked in a place. I am making a tic tac toe program and I want to register the click. i have been using this


                Vector2i winpos = Mouse::getPosition(window);
                int loc_x = winpos.x;
                int loc_y = winpos.y;

                INPUT:
                if (Mouse::isButtonPressed(Mouse::Left)) {
                       

                        if (72 < loc_x < 250 && 600 < loc_y < 500) {

                                cout << "works" << endl;
                               
                        }

                }
               
 
also i have placed this code inside the while loop
it should cout "works" in the cmd when i click in the first block but gives me "works" everytime i click inside
the window. it should not do that. it should only gimme works if mouse clicks in 1st cell/block.
look picture for clarification.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to detect if mouse clicked inside a certain box or rectangle
« Reply #1 on: April 16, 2019, 01:15:47 pm »
Is this C++? Because that's not how you check if a number is between 2 others in C++.  ???
random google result

SFML rectangles have a contains function to check if a point (the mouse position) is inside a rectangle.
« Last Edit: April 16, 2019, 01:17:26 pm by G. »

Chiggy_Playz

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to detect if mouse clicked inside a certain box or rectangle
« Reply #2 on: April 16, 2019, 01:20:17 pm »
Is this C++? Because that's not how you check if a number is between 2 others in C++.  ???
random google result

SFML rectangles have a contains function to check if a point (the mouse position) is inside a rectangle.

yeah that can really help me out but another problem i will face is that the 9 boxes you see arent 9 rectangles. they are 2 big rectangles. one placed horizontally and one placed vertically in the respective places and an additional on in the Centre coz one rectangle overlapped the other. get it? so what function can i use?
also i used the link you provided and tried that just now. doesnt cout "work" even once.
« Last Edit: April 16, 2019, 01:23:04 pm by Chiggy_Playz »