SFML community forums

Help => Graphics => Topic started by: MrMou6 on August 23, 2011, 03:47:12 pm

Title: click on sprite
Post by: MrMou6 on August 23, 2011, 03:47:12 pm
How i can do code:
when i click on my sprite... My program do something...
P.S. [Sorry for my english]
Title: click on sprite
Post by: rojan_neo on August 23, 2011, 05:33:42 pm
can you be more specific!! do you want some event to be triggered when mouse button is clicked ( at least that is what I understood) then in the main loop you add this code
Code: [Select]

//Here Win is your window
if(Win.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left))
{
Win.Close();
}



This is the case with all the input in SFML
Title: click on sprite
Post by: OniLinkPlus on August 23, 2011, 11:23:16 pm
Actually rojan, he wants his program to do something when he clicks a sprite. If you want to do this, you need to keep a container of all of your sprites, and when the mouse button is pressed, check if the coordinates of the mouse press are on top of any of these sprites.
Title: click on sprite
Post by: MrMou6 on August 28, 2011, 05:21:12 pm
Oni link can you give me example? because I'm a novice in SFML.
Title: click on sprite
Post by: MrMou6 on August 28, 2011, 09:41:51 pm
i try this:
Code: [Select]
if (sprite.GetPosition().x + sprite.GetSize().x == Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
but this is not work :(
Title: click on sprite
Post by: Nexus on August 28, 2011, 10:29:16 pm
This is completely wrong ;)

You are comparing a coordinate (a float number) with a boolean value (false or true). What do you want to achieve? To test whether the mouse is on a sprite, its x position has to be bigger than the beginning of the sprite (position) and smaller than the end of the sprite (position+size), same for y.

I think you should take a look at the tutorials and/or the documentation, they explain how you can get the mouse position from an sf::Event.
Title: click on sprite
Post by: MrMou6 on August 29, 2011, 10:26:03 am
sorry. i try this:
Code: [Select]
if (sprite.GetPosition().x * GetPosition().y == Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
but:
Code: [Select]
Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
is wrong..Nexus can you give me link to tut... How get mouse position..


Sorry for my english.
Title: click on sprite
Post by: Lo-X on August 29, 2011, 10:41:29 am
Humm...

You're multiplying the x position of a sprite by the y position of... of what ? Why are you multiplying them ? And why are you comparing the result (a number) to a boolean ?

All the tutorials about SFML 1.6 are on this website : www.sfml-dev.org

The right way is to detect when the user has clicked in the events loop, and then to compare X & Y coordinates of the click with a Rectangle coordinates that represents your sprite

Doc :
sf::Rect (1.6, 'cause 2.0 is different)
Tuto :
all :D
Title: click on sprite
Post by: OniLinkPlus on August 29, 2011, 05:51:02 pm
I'm curious: how good are you at math? Have you learned Geometry? Trigonometry?
Title: click on sprite
Post by: MrMou6 on August 29, 2011, 07:27:08 pm
triogonometry and geometry it's fine...
Title: click on sprite
Post by: OniLinkPlus on August 29, 2011, 08:21:14 pm
Quote from: "MrMou6"
triogonometry and geometry it's fine...
I'm sorry, I don't quite understand what you're trying to say.
Title: click on sprite
Post by: Damian on August 30, 2011, 04:58:55 pm
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!
Title: click on sprite
Post by: Nexus on August 30, 2011, 05:10:06 pm
Two small hints:
Code: [Select]
bool isSpriteClicked ( const sf::Sprite *spr, const sf::RenderWindow *render )
Code: [Select]
if (x)
    return true;
else
    return false;
you can directly write
Code: [Select]
return x;[/list]
Title: click on sprite
Post by: Damian on August 30, 2011, 05:14:35 pm
Ok. Thanks ;)
Title: click on sprite
Post by: MrMou6 on August 31, 2011, 08:19:10 am
Quote from: "Damian"

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!



OK!