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

Author Topic: click on sprite  (Read 20604 times)

0 Members and 1 Guest are viewing this topic.

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« 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]

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
click on sprite
« Reply #1 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

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
click on sprite
« Reply #2 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.
I use the latest build of SFML2

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« Reply #3 on: August 28, 2011, 05:21:12 pm »
Oni link can you give me example? because I'm a novice in SFML.

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« Reply #4 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 :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
click on sprite
« Reply #5 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« Reply #6 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.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
click on sprite
« Reply #7 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

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
click on sprite
« Reply #8 on: August 29, 2011, 05:51:02 pm »
I'm curious: how good are you at math? Have you learned Geometry? Trigonometry?
I use the latest build of SFML2

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« Reply #9 on: August 29, 2011, 07:27:08 pm »
triogonometry and geometry it's fine...

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
click on sprite
« Reply #10 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.
I use the latest build of SFML2

Damian

  • Newbie
  • *
  • Posts: 4
    • View Profile
click on sprite
« Reply #11 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!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
click on sprite
« Reply #12 on: August 30, 2011, 05:10:06 pm »
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.
Code: [Select]
bool isSpriteClicked ( const sf::Sprite *spr, const sf::RenderWindow *render )
  • Instead of
Code: [Select]
if (x)
    return true;
else
    return false;
you can directly write
Code: [Select]
return x;[/list]
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Damian

  • Newbie
  • *
  • Posts: 4
    • View Profile
click on sprite
« Reply #13 on: August 30, 2011, 05:14:35 pm »
Ok. Thanks ;)

MrMou6

  • Newbie
  • *
  • Posts: 17
    • View Profile
click on sprite
« Reply #14 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!