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

Author Topic: Movement  (Read 1859 times)

0 Members and 1 Guest are viewing this topic.

flachead

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Movement
« on: March 11, 2013, 09:01:03 pm »
Hello, i'm back for more  ::)

So my sprite moves to my mouse position ok, but when I let go of the mouse it reverts to its original position. I tried using sf::keyboard::iskeypressed in an if statement and the same thing happened. Am I missing something?

Quote
while ( mainWindow.isOpen() )
     {

       sf::Texture tFly;
       tFly.loadFromFile( "fly.png" );
       sf::Sprite sFly;
       sFly.setTexture( tFly );

         sf::Event event;

       while( mainWindow.pollEvent( event ) )
       {
         if( event.type == sf::Event::Closed )
            mainWindow.close();
       }
      
       if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
       {
            sFly.setPosition(sf::Mouse::getPosition(mainWindow).x, sf::Mouse::getPosition(mainWindow).y);
       }
 
         // Clear screen
         mainWindow.clear();
       mainWindow.draw( sFly );
 
         // Update the window
         mainWindow.display();
     }

Thanks very much in advance. ( I've googled everywhere and couldn't see anything related. )

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Movement
« Reply #1 on: March 11, 2013, 09:38:04 pm »
Why do you create a new Texture and Sprite in the game loop?
Sprites position is set at 0, 0 when you create it.

flachead

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Movement
« Reply #2 on: March 11, 2013, 10:25:45 pm »
I don't know i'm just using trial and error lol.

EDIT: I see, I took it out of the main loop and now it works fine. Boy do I feel stupid :( Thanks very much for your help!
« Last Edit: March 11, 2013, 10:28:35 pm by flachead »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Movement
« Reply #3 on: March 11, 2013, 11:59:32 pm »
I don't know i'm just using trial and error lol.
That may work for some simple task with a simple language, but C++ is not simple and game development is neither. You should get a good understanding of C++ by reading a good book, make sure to check the documentation and if face with a problem sit down and think about it before even doing anything with code. ;)

Btw lol. is not a valid way to finish sentences in English. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

flachead

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Movement
« Reply #4 on: March 12, 2013, 05:12:21 pm »
I have a pretty good understanding of C++, but the best way for me to learn is to make mistakes :) I also have a pretty good book here that I use for reference if I get stuck. Thanks for the info though I will check out your list of books, and stop using "lol" at the end of my sentences :) bad habit!