Hi,
currently I have two sprites - a spaceship without flames, and another with flames (when moved upwards). When I press up for first time, it changes to the sprite with flames and moves up without any issue but when I press up for a second time, it just disappears and I get a empty black screen. Anyone here knows why?
My code:
void Game::play(sf::RenderWindow &window) {
std::cout << "playing the game" << std::endl;
int startX = 500;
int startY = 400;
if (!texture.loadFromFile("pixil-test.png"))
{
// error...
std::cout << "doesnt work" << std::endl;
}
// first spaceship
spaceShip.move(startX, startY);
spaceShip.setTexture(texture);
spaceShip.setTextureRect(sf::IntRect(0, 0, 11, 17));
// second spaceship with rear flames
spaceShipMoveUp.setTexture(texture);
spaceShipMoveUp.setTextureRect(sf::IntRect(12, 0, 11, 17));
window.draw(spaceShip);
window.display();
while(window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed) {
window.close();
}
if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::Up) {
startY--;
window.clear();
spaceShipMoveUp.move(startX, startY);
window.draw(spaceShipMoveUp);
window.display();
}
else if(event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
}
}
}