Please make use of the code=cpp tag! ;)
This will work, but will throw you a warning at you up on compiling.
sf::FloatRect rectBounds = enemy.getGlobalBounds();
if (mouseX == rectBounds.left && mouseY == rectBounds.top)
{
hoverEnemy = true;
}
This won't throw a warning:
sf::FloatRect rectBounds = enemy.getGlobalBounds();
if (mouseX == static_cast<int>(rectBounds.left) && mouseY == static_cast<int>(rectBounds.top))
{
hoverEnemy = true;
}
But the coordinates won't get rounded up, so you could use:
sf::FloatRect rectBounds = enemy.getGlobalBounds();
if (mouseX == static_cast<int>(rectBounds.left+0.5f) && mouseY == static_cast<int>(rectBounds.top+0.5f))
{
hoverEnemy = true;
}
Then again this all seems a bit wrong and since you probably want some sort of collision test you'd better check if the mouse cursor is inside the rectangle:
if (enemy.getGlobalBounds().contains(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window))))
{
hoverEnemy = true;
}
Thanks it works for the most part. Somethings don't work though like:
Hover is always true even when the mouse is away from the square so when you click it always works no matter where you are.
I can't draw an object when it's hovered or clicked:
sf::FloatRect rectBounds = enemy.getGlobalBounds();
if (enemy.getGlobalBounds().contains(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window))))
{
hoverEnemy = true;
}
if (hoverEnemy = true)
{
std::cout << "Hover\n";
window.draw(enemyHover);
}
if (clickEnemy == true && hoverEnemy)
{
EnemyObject.die();
window.draw(enemyClicked);
std::cout << "Clicked";
clickEnemy = false;
}