Search code examples
sfml

Draggable sprite in the SFML


How I can make sprites drag with mouse, using code? I'm writing code for Battleships game, and I need codes for dragging with the mouse.


Solution

  • Think first what should happen in dragging,

    • Click the sprite
    • Move the mouse while still holding they left button on the sprite

    now that we have already listed that, we can now think on the second step, translating it into a pseudo code.

     if ( check here if the user is moving )
     {
         if ( check here if the user is holding left button )
         { 
             if ( check here if mouse is inside the sprite ) 
             {
                  set the position to the mouse pointer. 
             }
         }
     }
    

    did you get that? :)