SFML community forums
Help => General => Topic started by: flachead 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?
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. )
-
Why do you create a new Texture and Sprite in the game loop?
Sprites position is set at 0, 0 when you create it.
-
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!
-
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 (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), 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
-
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!