【问题标题】:Sprite isn't moving精灵不动
【发布时间】:2014-08-27 20:32:49
【问题描述】:

我是编程新手,因为我发现做起来更容易,所以我在玩 C++ 和 SFML。我试图让精灵移动,但由于某种原因,我做不到。我已经尝试了我找到的所有东西,但没有运气。任何人都知道为什么这不起作用?顺便说一句,我正在使用 Visual Studio 2012 Express 和 SFML 2.1。

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 800), "Project");
    window.setFramerateLimit(30);
    glEnable(GL_TEXTURE_2D);
    window.clear(sf::Color::White);
    window.display();
    sf::Texture charMain;
    if (!charMain.loadFromFile("Images/playerFrontSprite.png"))
    {
        return 1;
    }
    sf::Sprite charaMain;
    charaMain.setPosition(500.f , 500.f);
    charaMain.setTexture(charMain);
    window.draw(charaMain);
    window.display();
    sf::Font cavefont;
    sf::Font::Font(cavefont);
    if (!cavefont.loadFromFile("Cave-Story.ttf"))
        return 1;
        while (window.isOpen())
        {
            sf::Event playerAction;
            while (window.pollEvent(playerAction))
            {
                switch(playerAction.type)
                {
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::KeyPressed:
                    std::cout << "sf::Event::KeyPressed" << std::endl;
                    switch(playerAction.key.code)
                    {
                    case sf::Keyboard::W:
                        charaMain.move(0 , 1);
                        break;
                    case sf::Keyboard::A:
                        charaMain.move(-1 , 0);
                        break;
                    case sf::Keyboard::S:
                        charaMain.move(0 , -1);
                        break;
                    case sf::Keyboard::D:
                        charaMain.move(1 , 0);
                        break;
                    }
                    break;
                }
            }
            }
}

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    您的问题是您正在移动对象,但您没有在屏幕上的新位置绘制它。你的绘图代码在主循环之外,所以在你移动精灵后它永远不会被调用,所以你永远看不到效果。

    您基本上需要在事件处理循环之外的while (window.isOpen()) 循环内拉取渲染代码(即window.draw(charaMain)window.display())。

    【讨论】:

    • @Aerdenhein:没问题!您可能需要单击投票框下方的勾号,将其标记为您接受的答案 - 这可以让其他人知道您的问题已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多