Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Checking if the mouse is on a sprite  (Read 10400 times)

0 Members and 1 Guest are viewing this topic.

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Checking if the mouse is on a sprite
« on: March 31, 2015, 10:56:49 am »
So in all of my SFML development, i've been doing manual checks to see if the mouse is currently hovering over a sprites area by comparing the current positions to the outer sides of the sprite. This is kind of annoying after a while if you have many buttons, and I'm just wondering if there was any function that I could use in order to get this result, some thing like mouseOnSprite(SpriteName);

Thanks

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Checking if the mouse is on a sprite
« Reply #1 on: March 31, 2015, 10:58:11 am »
You can build such a function by combining sf::Sprite::getGlobalBounds() and sf::Rect::contains() :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Checking if the mouse is on a sprite
« Reply #2 on: March 31, 2015, 11:12:04 am »
Thank you, this will help a lot! :)

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Checking if the mouse is on a sprite
« Reply #3 on: March 31, 2015, 11:53:19 am »
You can build such a function by combining sf::Sprite::getGlobalBounds() and sf::Rect::contains() :)

I'm having a problem tho. The getGlobalBounds function returns a sf::Rect<float>. I have no idea how to convert it to a regular float, and there doesn't seem to be anything online.

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Checking if the mouse is on a sprite
« Reply #4 on: March 31, 2015, 12:11:02 pm »
Why do you want to convert sf::Rect<float> to simple float?

Just use
yourRect.contains(MousePosition) == bool

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Checking if the mouse is on a sprite
« Reply #5 on: March 31, 2015, 03:28:31 pm »
there doesn't seem to be anything online.

What do you mean? The sf::Rect class is fully documented here: http://www.sfml-dev.org/documentation/2.2/classsf_1_1Rect.php

Bushider

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Checking if the mouse is on a sprite
« Reply #6 on: April 04, 2015, 09:45:41 pm »
I have almost the same question. I'm just learing SFML, and copying a code from youtube tutorial. I've did everything exactly the same as the tutorial creator, and his code was compiled my wasn't.

It's like that :

Quote
ivoid MainMenu::Update(FrameWork &frame, sf::RenderWindow &updatetarget, float frametime)
{
   if (button[0].getGlobalBounds().contains(sf::Mouse::getPosition(updatetarget).x, sf::Mouse::getPosition(updatetarget).y) &&
      (button[0].getColor() != sf::Color::Red))
   {
      button[0].setColor(sf::Color::Blue);
      playselect = true;
   }
   else if (!button[0].getGlobalBounds().contains(sf::Mouse::getPosition(updatetarget).x, sf::Mouse::getPosition(updatetarget).y)
      && (button[0].getColor() == sf::Color::White)
   {
      button[0].setColor(sf::Color::White);
      playselect = false;
   }
}


Where button is array of text. Error occures in place where i check if mouse position is on texture. lines :
Quote
if (button[0].getGlobalBounds().contains(sf::Mouse::getPosition(updatetarget).x, sf::Mouse::getPosition(updatetarget).y)

!button[0].getGlobalBounds().contains(sf::Mouse::getPosition(updatetarget).x, sf::Mouse::getPosition(updatetarget).y)

Type of error :

Quote
syntax error : '::'
'(' : illegal token on right side of '::'

Could anyone help me ? I'm new to programing. :)
Thanks a lot!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Checking if the mouse is on a sprite
« Reply #7 on: April 05, 2015, 12:00:23 am »
You should create your own thread unless it's really about exactly the same thing and use the [code=cpp][/code] tags.
Next you should gather more experience with C++ and compiler errors if you can't figure out what the error message means... ;)

else if (!button[0].getGlobalBounds().contains(sf::Mouse::getPosition(updatetarget).x, sf::Mouse::getPosition(updatetarget).y)
      && (button[0].getColor() == sf::Color::White)

You never close the if( statement.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything