Position Equality

I’m trying to write something like that:

if(sprite->getPositionX() = visibleSize.height)
{
     something();
}

But it doesn’t work. Is there any way to solve this problem?

Why not add a few cout to see why?

Comparing position to height is interesting but explain or show what you want to do please.

Oh sorry, that was visibleSize.width :smiley:

My objects spawn on the right side of the screen and will move to the left. and I want to spawn new object immediately after each activated object enters the game screen.

Position is float so either compare using some precision or cast to integer.
But in your case just check x > width

1 Like

If you’re testing for equality, you should be using ==, not =, and I assume that’s just a typo in your example, since the compiler should have thrown an error if you actually tried that code.

Anyhow, the actual problem is that you shouldn’t check if the values are equal, since it is likely to be off by a few pixels (or points if you’re using scaling), depending on how quickly the sprite is moving.

It’s better to use >= or <=, depending on what you’re testing for:

if (sprite->getPositionX() >= visible.width)
{
    ...
}

Also, getPositionX() isn’t the best value to use either, since it’s affected by your sprite’s anchor point.

Try instead to use getBoundingBox() :

const auto& boundingBox = sprite->getBoundingBox();
if (boundingBox.getMinX() <= visibleSize.width)
{
    ....
}

Or you can use getMaxX(), getMidX() etc, depending on which part of the sprite you want to check for.

5 Likes

Hi. checking equality on floats not working as you imagine. try cast them to int and then check the equality on int values.