Both if and else staments are being performed?

I've come across an annoying bug where both my if statement and the following else statement are being done, like so:
1
2
3
4
5
6
if(conditionA == true && conditionB == false){
std::cout << "do stuff";
}
else{
std::cout << "don't do stuff";
}
Both "do stuff" and "don't do stuff" are being printed and it is very frustrating. That's the pseudocode, but the full details are that I am using sfml and trying to print "do stuff" if the cursor is inside the window and not touching any given objects on screen, and print "don't do stuff" otherwise (assuming that the cursor is either offscreen or touching an object). Can anybody help me out?
You probably made some mistake but it's impossible to say by just looking at "pseudocode".
You're right, here is the actual code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//create a .01 by .01 position of mouse used for checking for float rect collisions
	sf::FloatRect mousefr(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y, .01f, .01f);
	//good, except the collision test doesn't need to happen this many times per loop
	bool said = false;
	bool said2 = false;
	for(int i = 0; i < board.GetCollObjects().size(); i++){
		if(screen.intersects(mousefr) && !board.GetCollObjects()[i].intersects(mousefr)){
			//MoveFlashlight(mousefr);
			if(said == false){
				std::cout << "mouse being drawn";
				said = true;
			}
			board.GetFlashlight().setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
		}
		else if(screen.intersects(mousefr) && board.GetCollObjects()[i].intersects(mousefr)){
			if(said2 == false){
				std::cout << "\n\n\n";
				said = true;
			}
		}

It's just hard to post all the code without posting tens or hundreds of lines, so I tried to simplify. The booleans said and said2 are just for debugging, nothing important. So, when the mouse is hovering over one of the collision objects, both the if and else statements are happening, but if the mouse is over empty space then just the if statement is happening.
Last edited on
If you have multiple objects you probably end up in the else part for some of them because you probably can't hover all of them at the same time?
If you have multiple objects you probably end up in the else part for some of them because you probably can't hover all of them at the same time?


Wow, I can't believe I missed that. I'll choc that up to my head injury :p. Anyways, for the curious, here is my reorganized code that does not indeed perform both the if and the else (I had to give the boolean a more global scope as well)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//create a .01 by .01 position of mouse used for checking for float rect collisions
	sf::FloatRect mousefr(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y, .01f, .01f);
	if(screen.intersects(mousefr)){
		for(int i = 0; i < board.GetCollObjects().size(); i++){
			if(board.GetCollObjects()[i].intersects(mousefr) && board.GetCollide() == false){
				board.SetCollide(true);
			}
		}
		if(board.GetCollide() == false){
			board.GetFlashlight().setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
		}
		else{
			if(CollisionTest(mousefr) == true){
				board.SetCollide(false);	
			}
		}
Topic archived. No new replies allowed.