Reputation: 495
int Game::MouseOnDot(float x, float y, RenderWindow &renderWindow) {
Rect<int> Dot;
Event event;
Dot.left = x;
Dot.top = y;
Dot.width = 20;
Dot.height = 20;
while (renderWindow.pollEvent(event)) {
float Mx = sf::Mouse::getPosition().x;
float My = sf::Mouse::getPosition().y;
if (event.type == Event::MouseButtonReleased&&Mx > x && Mx < Dot.height && My > y && My < Dot.width){
return 1;
}
else
return 0;
}
}
I don't know why this won't work if the button is pressed on the dot it returns 1 which tell other function to close window. Am i doing something wrong on mouse position?
while (renderWindow.isOpen()) {
processEvents(renderWindow);
float Time = clock.getElapsedTime().asSeconds();
float TimeDifference = Time - LastUpdateTime;
if (TimeDifference >= UpdateTime) {
processEvents(renderWindow);
y += 3;
if (y <= 770) {
if(Game::MouseOnDot(x, y, renderWindow)==1)
renderWindow.close();
Game::Spawn(renderWindow, Green_Dots, x, y);
LastUpdateTime = Time;
return;
Still don't work i paste here the part when MouseOnDot reutrns 0 or 1. It won't close the windows and i don't know why??
Upvotes: 0
Views: 19614
Reputation: 61
Using sf::Mouse::getPosition().x returns the position relative to the desktop, you if you want it relative to your renderWindow you need to do: sf::Mouse::getPosition(renderWindow).x
then Attila is exactly right about the mouse/dot comparison :)
Upvotes: 6
Reputation: 28802
I think your problem is that you compare the position to the x coordinate and the height. You need to compare to x and x+height (similarly for y coordinate/width)
Try:
if (event.type == Event::MouseButtonReleased &&
Mx > x && Mx < x + Dot.height &&
My > y && My < y + Dot.width) {
//...
}
Upvotes: 0