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

Author Topic: Click on a sprite  (Read 14946 times)

0 Members and 1 Guest are viewing this topic.

synok

  • Newbie
  • *
  • Posts: 18
    • View Profile
Click on a sprite
« on: April 16, 2013, 05:59:21 pm »
Hello everyone,

I am using SFML 2.0 to try to click on a sprite. Tried using IntRect, Vector2, etc with no avail. All I need is
a simple way to click on an image. I tried googling like crazy with no results either.

The idea is if the mouse has been pressed, it needs to check if you are within a rectangle. If so, the button is clicked. Else, nothing will happen.

Thanks,

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Click on a sprite
« Reply #1 on: April 16, 2013, 06:00:37 pm »
Where are you stuck exactly?

Also, try the forum search. This topic has already been discussed multiple times.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

synok

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Click on a sprite
« Reply #2 on: April 16, 2013, 06:06:52 pm »
I tried searching, but did not find anything relevant.

if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
        // Here is where it checks if the mouse is inside the rectangle / sprite.
}

I need to click a sprite and have things happen when I do.

Vovosunt

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Click on a sprite
« Reply #3 on: April 16, 2013, 06:35:05 pm »
I tried searching, but did not find anything relevant.

Blatant lies =_="
http://bit.ly/14tfCkE

synok

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Click on a sprite
« Reply #4 on: April 16, 2013, 07:57:39 pm »
I want to make something clear.

Avoiding the question by ridiculing it does not solve my problem. I have tried to specify what you asked, with no solution, that is why I specifically logged in and asked here. If you are a serious user of this community, provide something useful that I did not manage to find instead of pointing out that tons of this information already exists.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Click on a sprite
« Reply #5 on: April 16, 2013, 08:32:28 pm »
I browsed the Google results and couldn't find the correct answer in them. When you point to lmgtfy, at least make sure that the solution is within the first results ;)

if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    // transform the mouse position from window coordinates to world coordinates
    sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));

    // retrieve the bounding box of the sprite
    sf::FloatRect bounds = sprite.getGlobalBounds();

    // hit test
    if (bounds.contains(mouse))
    {
        // mouse is on sprite!
    }
}
Laurent Gomila - SFML developer

synok

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Click on a sprite
« Reply #6 on: April 16, 2013, 08:51:43 pm »
I agree. Thank you for the snippet! :-)
« Last Edit: April 16, 2013, 08:55:17 pm by synok »

Vovosunt

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Click on a sprite
« Reply #7 on: April 18, 2013, 11:32:16 am »
Actually it was literally the very first result, that's why I got a bit ticked off.
Maybe my Google settings are different... >_>
The first result for me is this: http://en.sfml-dev.org/forums/index.php?topic=5662.0
And what you get from it is this:
Code: [Select]
bool isSpriteClicked ( sf::Sprite *spr, sf::RenderWindow *render )
{
const sf::Input& Input = render->GetInput();
if( Input.GetMouseX() > spr->GetPosition().x
&& Input.GetMouseX() < spr->GetPosition().x + spr->GetSize().x
&& Input.GetMouseY() > spr->GetPosition().y && Input.GetMouseY() < spr->GetPosition().y + spr->GetSize().y
&& Input.IsMouseButtonDown(sf::Mouse::Left)
)
{
return true;
}
else
{
return false;
}
}

Usage:
Code: [Select]
if(isSpriteClicked( &YOUR_SPRITE, &Window ))
{
// Example:
Window.Close();
}

It should work correctly with SFML 1.6 and VS 2008/2010

I helped you. But i see that you dont have any idea on game prgramming ;). At first you must know C++ better and Do some console games (CLI). Text based. Dont try make 2d/3d games right now! Learn at first!

As Nexus mentioned, this topic was grinded over many times before, there is no point in doing it once more, just because someone is too lazy to spend around 10 seconds to search for the old topic.

BTW, synok the last few sentence in the quote above apply to you too.
Do some tutorials or read some books on game development, before you start programming.

if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
   ...
}

He should learn to do it without all this fancy stuff, otherwise it is useless.
I mean AABB collisions are one of the very first things you learn, seriously =_="

Hehe, let's make it even shorter  :P :
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    if (sprite.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window))))
    {
        // mouse is on sprite!
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Click on a sprite
« Reply #8 on: April 18, 2013, 11:41:05 am »
Quote
Actually it was literally the very first result, that's why I got a bit ticked off.
He's using SFML 2. All the results that you can find are for SFML 1.6, and the solution is so much different between these two versions that they are not relevant.
Laurent Gomila - SFML developer