c++ - SFML C++ 不渲染

标签 c++ variables rendering sfml

我遇到了一个问题,我正在写一个小游戏,有几次当我添加新变量如 float 或 sf::Vector2f 时,sfml 没有渲染其他元素或不移动(或其他奇怪的问题)。

这是我的主要功能:

const int resW=800, resH=600;
sf::RenderWindow app(sf::VideoMode(resW, resH), "Jump");//, sf::Style::Fullscreen);

int main()
{
    //FPS
    app.setFramerateLimit(60);
    sf::Font font;
    float jumpStrength = 0.0;

    //when i uncomment it, my player is not rendered.
    //When it's commented everything is rendered
    //some time ago I had declared float number in player class,
    //and then player couldn't move (problem with sfml setPosition()???
    //sf::RectangleShape strengthRect(sf::Vector2f(100,100));
    while (app.isOpen())
    {
        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed ||
                sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
                app.close();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
            {
                jumpStrength+=0.2;
            }
            else if(jumpStrength>0.0)
            {
                player.Jump(jumpStrength);
            }
        }

        // Clear screen
        app.clear();

        ChandleInput();
        ChandleCollisions();

        player.Update();
        player.Draw(&app);
        app.draw(strengthRect);
    }
}

class CPlayer
{
    private:

    sf::Texture playerTex[3];
    sf::Sprite player;

    float g;
    float height;
    float jumpVel;

    void ResetJump() {jumpVel = 11;
            g = 0.2;
            height = 0;}
            void Move(float x, float y)
            {
                sf::Vector2f vec = player.getPosition();
                vec.x += x;
                vec.y += y;
                player.setPosition(vec.x,vec.y);
            }
    public:
        CPlayer()
        {
        if (!playerTex[0].loadFromFile("Images/frame_1.png"))
            return;// EXIT_FAILURE;
        if (!playerTex[1].loadFromFile("Images/frame_2.png"))
            return;// EXIT_FAILURE;
        if (!playerTex[2].loadFromFile("Images/frame_3.png"))
            return;// EXIT_FAILURE;
            player.setTexture(playerTex[0]);
                int x,y;
    int w,h;
            w = player.getLocalBounds().width;
            h = player.getLocalBounds().height;
            x = resW/2;y=resH-h/2;
            x-=w/2;
            y-=h/2;
            player.setPosition(x,y);
        }
        int GetX(){return player.getPosition().x;}
        int GetY(){return player.getPosition().y;}
        float GetHeight(){return height;}
        void Update()
        {
            if(height>0)//skacze
            {
                jumpVel -= g;
                height += jumpVel;
            }
            else
            {
                if(height<0)
                {
                    height = 0;
                }
                if(jumped)
                {
                    landed = true;
                    player.setTexture(playerTex[0]);
                }
                //Move(0,-1);
            }
        }
        void Jump(float strength)
        {
            if(!jumped)
            if(height<=0)//nie skacze
            {
                ResetJump();
                height = jumpVel;
                jumped = true;
                player.setTexture(playerTex[1]);
                jumpVel = strength;
            }
        }
        void Draw(sf::RenderWindow *app)
        {
                Move(0.0,-height);
                app->draw(player);
                Move(0.0,height);

        }
};

编辑

我注意到当我打电话时(在我的玩家类(class)) player.setPosition(...); 播放器未呈现。当我不调用它时,它会被渲染。 另一方面,当我不声明 RectangleShape 时,播放器会被正确移动和呈现。

编辑 2 在类 CPlayer 中是 Move() 方法,其中是 setPosition()。

最佳答案

当遇到这种错误/故障时,游戏的行为会根据变量的数量或数组中的项目而改变,这很可能是由于变量未正确初始化引起的。

在它们的所有构造函数中为所有类中的所有属性赋值。

在你的例子中,g、height 和 jumpVel 没有被初始化。在构造函数中给它们一个默认值,并检查你的其他类是否在构造函数中初始化了它们的属性。

关于c++ - SFML C++ 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347987/

相关文章:

c++ - 是抽象类还是纯虚(接口(interface))?

c++ - 如何将 wstring 转换为字符串作为转义符

c++ - 在 OpenGL C++ 中检测鼠标点击

javascript - 使用变量增量在 Javascript 中创建新变量

bash - 什么意思#!在 ${variable#!} 中? ( bash )

c++在将变量传递给函数时声明变量

ajax - JSF2 使用 ajax 更新复合元素

c++ - Namespace::* 和传统的 * 有什么区别吗?

更新 Google Chrome (v.28) 后的 CSS 菜单呈现问题

error-handling - Yii2 : Display Error Messages when using forms inside Pjax