1
Graphics / Question about sprite moving
« on: August 10, 2013, 10:27:07 am »
I can keep the sprite on the screen, but my problem is when it reaches the end of screen, it cant go back left, And if I continue pressing key it lefts the screen.
Quote
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow Window(sf::VideoMode(320,320),"");
sf::Texture tex;
tex.loadFromFile("bullet.png");
sf::Sprite sprite(tex);
float movespeed = 0.1f;
float velocityX = 0;
while(Window.isOpen())
{
Window.clear();
Window.draw(sprite);
Window.display();
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
}
}
if(sprite.getPosition().x + 9 > 320 || sprite.getPosition().x < 0)
velocityX = 0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
velocityX = movespeed;
sprite.move(velocityX,0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
velocityX = -movespeed;
sprite.move(velocityX,0);
}
}
}