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

Author Topic: sprite move issue  (Read 4816 times)

0 Members and 1 Guest are viewing this topic.

geekz

  • Newbie
  • *
  • Posts: 7
    • View Profile
sprite move issue
« on: August 31, 2021, 12:33:26 pm »
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();
                }
            }
        }
    }
}
« Last Edit: August 31, 2021, 12:39:08 pm by geekz »

geekz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sprite move issue
« Reply #1 on: August 31, 2021, 05:43:26 pm »
edit: solved the issue using spaceShipMoveUp.position() instead of .move()

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sprite move issue
« Reply #2 on: August 31, 2021, 06:37:48 pm »
Btw. you should not have your clear/draw/display inside the event loop, otherwise you screen only renders a new frame if there's an event.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

geekz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sprite move issue
« Reply #3 on: September 02, 2021, 11:05:25 am »
Btw. you should not have your clear/draw/display inside the event loop, otherwise you screen only renders a new frame if there's an event.
Yes, that's what I want right now but I'll change frame render later when there's no event - just testing stuff at this moment.