if and else running at the same time

Pages: 12
Here is a bit of summary of my problem:

I am making a game. I have an abstract class called GameObject, and two child classes called Player, and Block. Each extended class from GameObject has an ID (Enumeration). I also have a vector of GameObjects. For collision, in my player class. I go through every GameObject in my list to check collision for every object. Like so :

for(unsigned i = 0; i < handler->getGameObjects().size(); i++){
GameObject& tempObject = handler->getGameObject(i);

if(tempObject.getId() == ObjectID::BLOCK) { // collision with block
if(this->getBoundsBottom().intersects(tempObject.getBoundsAll()){
// what happens if the player's bottom collides with the block.
std::printf("%s", "Bottom \n");
} else {
std::printf("%s", "Not bottom \n");
}
}
}


this is happening in the Player's inherited method update(), (Every frame).

How ever this gives me this result in the console:

Bottom
Not bottom
Bottom
Not bottom
Bottom
Not bottom
Bottom
Not bottom
.... and so on.



HOWEVER if I do this exact same process outside of the for loop and use "handler->getGameObject(0).getBoundsAll()" instead of "tempObject" (0 is the index of the block I am trying to collide with), everything seems to work good.
What is this problem?


Your if statement is missing a )
What does this print?
1
2
3
4
5
6
7
8
9
for (const GameObject& obj: handler->getGameObjects()) {
    if (obj.getId() == ObjectID::BLOCK) {
        std::cerr << "Bottom?  ";
        if (getBoundsBottom().intersects(obj.getBoundsAll()))
            std::cerr << "yes\n";
        else
            std::cerr << "no\n";
    }
} 

I will check, then I will inform you, thanks :)
Nope, still the same problem, this is what it prints:

Bottom? no
Bottom? yes
Bottom? no
Bottom? no
Bottom? yes
Bottom? no
Bottom? no
Bottom? yes
Bottom? no

... and so on
Did you fix the missing ) ?
Sorry, where was that error?
But that's different from your previous result.
It was: yes no yes no yes no ...
Now it's: no yes no no yes no no yes ...
So it's not exactly the same problem.

@Repeater: Would it even compile with that missing closing paren?

@hassanAman: Was that missing closing paren on this line a typo? Is it really in your code?
if(this->getBoundsBottom().intersects(tempObject.getBoundsAll()){ <-- needs another ) before {
Last edited on
No, no, everything in your code matched with my code. It was perfect.
Sometimes the result is :

Bottom
Not bottom
Bottom
Not bottom

Or

Bottom
Bottom
Not bottom
Bottom
Bottom
Not bottom

etc, It just keeps giving some bizarre patterns...

@Repeater: Would it even compile with that missing closing paren?


We can't see the whole code. We know that what we've been shown will not compile. So we know that whatever the OP is compiling, it's not what we've been shown...
And yes that was a typo, I didn't exactly copy paste :)

if(this->getBoundsBottom().intersects(tempObject.getBoundsAll()){ // <--- that last paren was a typo, my bad
<--- that last paren was a typo, my bad


That last ) needs to be there. It's not a typo.
Last edited on
Yes, I placed that paren now :)
@Repeater, right. Good point. We definitely need to see more code.

@hassanAman, you'll need to put the whole code up somewhere for download.
Like, the whole Project?
Surely, I will do that.
I will put the Header and Source files
The files are now available, please check them here : https://files.fm/u/wgqukvnu

Note to other helpers: The project is well-organized and not very big, so it's not too much of a chore to take a look at it. Each file is quite small.

At the download site, after clicking on the first big square (that says "view" when you hover over it) the next page has a red Download link to get a zip file.

The code in question is in Player.cpp, lines 20 to 28.

@hassanAman, What do you expect that it should print?
Last edited on
I expect that when the Bottom of the player is colliding, it prints "Bottom", and when it is not, it prints "Not bottom".
Pages: 12