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

Author Topic: Dragging Sprite outside original position  (Read 2695 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Dragging Sprite outside original position
« 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.

Code: [Select]
#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;
 }

tobybear

  • Newbie
  • *
  • Posts: 27
    • View Profile
Dragging Sprite outside original position
« Reply #1 on: January 26, 2012, 10:36:06 am »
Code: [Select]

     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!

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Dragging Sprite outside original position
« Reply #2 on: January 26, 2012, 04:43:39 pm »
Thanks man. That fixed it :)

Edit:  So how would I do the following as well?
Quote
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.

tobybear

  • Newbie
  • *
  • Posts: 27
    • View Profile
Dragging Sprite outside original position
« Reply #3 on: January 26, 2012, 07:37:26 pm »
Quote from: "LucasShadow"
Thanks man. That fixed it :)

Edit:  So how would I do the following as well?
Quote
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.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Dragging Sprite outside original position
« Reply #4 on: January 26, 2012, 08:34:43 pm »
Quote
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.
TGUI: C++ SFML GUI

 

anything