SFML community forums
Help => Graphics => Topic started by: LucasShadow on January 26, 2012, 01:55:18 am
-
So I have found quite a few topics on this in the forums, but have not been able to implement a successful solution to the problem. When holding down the mouse button over the sprite, it moves, but only as far as the parameters of the original sprite's position(somewhere around 35 pixels it seems). Another problem I am having is figuring out how to make it so that, if the mouse button is down and then the cursor is rolled over the sprite, it moves it. Is there a way to make it so that that doesnt happen? So that if Sprite1 is being dragged over Sprite2, then Sprite2 is also not dragged along with Sprite1. Any help is much appreciated :)
Oh, and below is my current code for dragging a single sprite.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600), "Dragging Test");
sf::Image SpriteImg;
if (!SpriteImg.LoadFromFile("sprite.png"))
return EXIT_FAILURE;
sf::Sprite SpriteObj(SpriteImg);
SpriteObj.SetPosition(365, 265);
SpriteImg.SetSmooth(false);
float XPosSprite = SpriteObj.GetPosition().x;//the x position of the panel
float YPosSprite = SpriteObj.GetPosition().y;//the y position of the panel
float XSizeSprite = SpriteObj.GetSize().x;//the width of the panel
float YSizeSprite = SpriteObj.GetSize().y;//the height of the panel
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
float MouseX = App.GetInput().GetMouseX();
float MouseY = App.GetInput().GetMouseY();
if((MouseX >= XPosSprite && MouseX <= XPosSprite+XSizeSprite && MouseY >= YPosSprite && MouseY <= YPosSprite+YSizeSprite) == true)
{
SpriteObj.SetPosition(MouseX - (XSizeSprite / 2), MouseY - (YSizeSprite / 2));
}
}
App.Clear();
App.Draw(SpriteObj);
App.Display();
}
return EXIT_SUCCESS;
}
-
float XPosSprite = SpriteObj.GetPosition().x;//the x position of the panel
float YPosSprite = SpriteObj.GetPosition().y;//the y position of the panel
float XSizeSprite = SpriteObj.GetSize().x;//the width of the panel
float YSizeSprite = SpriteObj.GetSize().y;//the height of the panel
You have to put those in your main loop as you are constantly changing the position of SpriteObj!
-
Thanks man. That fixed it :)
Edit: So how would I do the following as well?
Another problem I am having is figuring out how to make it so that, if the mouse button is down and then the cursor is rolled over the sprite, it moves it. Is there a way to make it so that that doesnt happen? So that if Sprite1 is being dragged over Sprite2, then Sprite2 is also not dragged along with Sprite1.
-
Thanks man. That fixed it :)
Edit: So how would I do the following as well?
Another problem I am having is figuring out how to make it so that, if the mouse button is down and then the cursor is rolled over the sprite, it moves it. Is there a way to make it so that that doesnt happen? So that if Sprite1 is being dragged over Sprite2, then Sprite2 is also not dragged along with Sprite1.
I don't know what you mean exactly. Your example only contains one sprite and since you change the position of a specific sprite, others shouldn't be affected.
-
So that if Sprite1 is being dragged over Sprite2, then Sprite2 is also not dragged along with Sprite1.
You should have a variable to store which sprite is being dragged.
It is e.g. 0 when you aren't dragging, 1 when dragging Sprite1, ...
When you start dragging Sprite1 the value will become 1. You then move over Sprite2 and it must check if the variable is 2.
This is not the case so Sprite2 will not be moved.
I hope this makes any sense.