SFML community forums
Help => Window => Topic started by: Crash47 on October 08, 2010, 08:24:39 am
-
Hi!
I'm having problem with MouseOver function.
I want to make:
If mouse is over object and I click left mouse button
// Do something
Code:
bool MouseOver(sf::Test &Test)
{
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) /*Do something*/();
}
-
What is exactly your problem (how this mouse over function is called?...) ? What have you done so far ?
-
What is exactly your problem (how this mouse over function is called?...) ? What have you done so far ?
Thanks for reply. I'm having problem with function MouseOver. I don't know how to use it.
bool MouseOver(sf::Test &Test)
{
// Do something
}
Test is sprite, yes?
-
Where does this function come from? It's not part of SFML.
-
Code:
bool MouseOver(sf::Test &Test)
{
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) /*Do something*/();
}
Well, you need the position of the sprite and your mouse, too. It's a simple collision detection (google for 2D Collision Detection, you'll find a lot). It's more like this:
bool isPointOverSprite(const sf::Vector2f Position, const sf::Sprite &Sprite)
{
return (Position.x < Sprite.GetPosition().x + Sprite.GetSize().x) && (Sprite.GetPosition().x < Position.x) &&
(Position.y < Sprite.GetPosition().y + Sprite.GetSize().y) && (Sprite.GetPosition().y < Position.y);
}
In your case - Position.X would be
App.GetInput().GetMouseX()
Position.Y of course
App.GetInput().GetMouseY()
With the function above you could now use it like that:
sf::Vector2f mousePosition = new sf::Vector2f(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && isPointOverSprite (mousePosition, yourSprite)