SFML/C++ Sprite Disappearing After Certain Point

时间:2016-07-11 23:18:37

标签: c++ sprite sfml

int main() {
sf::RenderWindow window;

sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);

window.create(sf::VideoMode(900, 900), "SFML Game", sf::Style::Titlebar | sf::Style::Close);
window.setPosition(centerWindow);

window.setKeyRepeatEnabled(true);

sf::Texture wallTxture;
sf::Sprite wall;
if (!wallTxture.loadFromFile("wall.png")) {
    std::cerr << "Error\n";
}
wall.setTexture(wallTxture);

//Gravity Vars:
int groundHeight = 750;
bool isJumping = false;

//Movement Vars:
bool goingRight = false;
bool goingLeft = false;

//Set View Mode:
sf::View followPlayer;
followPlayer.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));

Player player("newPlayer.png");
player.setPos({ 800, 800 });

sf::Vector2f position(window.getSize().x / 2, window.getSize().y / 2);

//Main Loop:
while (window.isOpen()) {

    const float moveSpeed = 0.1;

    sf::Event Event;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
        isJumping = true;
        //Handle Movement While Jumping:
        if (goingLeft == true) {
            player.move({ -moveSpeed, -moveSpeed });
        }
        else if (goingRight == true) {
            player.move({ moveSpeed, -moveSpeed });
        }
        else {
            player.move({ 0, -moveSpeed });
        }
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
        player.move({ 0, moveSpeed });
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
        player.move({ -moveSpeed, 0 });
        goingLeft = true;
        player.flipX('l');
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        player.move({ moveSpeed, 0 });
        goingRight = true;
        player.flipX('r');
    }

    //Event Loop:
    while (window.pollEvent(Event)) {
        switch (Event.type) {

        case sf::Event::Closed:
            window.close();

        case sf::Event::KeyReleased:
            if (Event.key.code == sf::Keyboard::Up) {
                isJumping = false;
            }
            else if (Event.key.code == sf::Keyboard::Left) {
                goingLeft = false;
            }
            else if (Event.key.code == sf::Keyboard::Right) {
                goingRight = false;
            }
        }

    }

    if (player.getX() > window.getSize().x) {
        position.x = player.getX();
    }
    else {
        position.x = window.getSize().x;
    }

    //If player is in air and not jumping:
    if (player.getY() < groundHeight && isJumping == false) {
        player.move({ 0, moveSpeed });
    }

    followPlayer.setCenter(position);

    window.clear();
    window.setView(followPlayer);
    window.draw(wall);
    player.drawTo(window);
    window.display();
}
}

this is my code. What I am trying to do is create a 2D platformer sidescroller. Everything works, except when the sprite goes past a certain point it'll just disappear. This also happens when I jump and move through the air at the same time. I cannot figure out why this is happening, any help would be greatly appreciated :)

1 个答案:

答案 0 :(得分:1)

我通过摆脱我的flipX函数来解决问题,而只是创建一个面向不同方向的新精灵,然后每次用户面向新方向时更改它