SFML community forums

Help => General => Topic started by: Narok on August 10, 2012, 03:38:03 am

Title: How do I convert a getBlobalBounds() to a int like thing
Post by: Narok on August 10, 2012, 03:38:03 am
I'm trying to make it so when I click on a shape it disapears:

      sf::FloatRect rectBounds = enemy.getGlobalBounds();

      if (mouseX == rectBounds && mouseY == rectBounds)
      {
         hoverEnemy = true;
      }

      if (clickEnemy == true && hoverEnemy)
      {
         EnemyObject.die();
         clickEnemy = false;
      }

This ^ obviously doenst work. Is there a way I can do it?
Title: Re: How do I convert a getBlobalBounds() to a int like thing
Post by: eXpl0it3r on August 10, 2012, 03:47:31 am
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;
}
Title: Re: eXpl0it3r
Post by: Narok on August 10, 2012, 06:07:06 am
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;
                }
Title: Re: How do I convert a getBlobalBounds() to a int like thing
Post by: eXpl0it3r on August 10, 2012, 09:19:02 am
Because hoverEnemy = true is an assignment which will always be true. ;)
Don't use == true checks for booleans a simple if(hoverEnemy) is enough.