SFML community forums
Help => Graphics => Topic started 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]
-
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
//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
-
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.
-
Oni link can you give me example? because I'm a novice in SFML.
-
i try this:
if (sprite.GetPosition().x + sprite.GetSize().x == Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
but this is not work :(
-
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.
-
sorry. i try this:
if (sprite.GetPosition().x * GetPosition().y == Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
but:
Window.GetInput().IsMouseButtonDown(sf::Mouse::Left))
is wrong..Nexus can you give me link to tut... How get mouse position..
Sorry for my english.
-
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
-
I'm curious: how good are you at math? Have you learned Geometry? Trigonometry?
-
triogonometry and geometry it's fine...
-
triogonometry and geometry it's fine...
I'm sorry, I don't quite understand what you're trying to say.
-
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:
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!
-
Two small hints:
- You could use pointers to const. Then, you don't accidentally change the passed variables, and the caller can also pass constant objects.
bool isSpriteClicked ( const sf::Sprite *spr, const sf::RenderWindow *render )
if (x)
return true;
else
return false;
you can directly write
return x;
[/list]
-
Ok. Thanks ;)
-
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!