C++ 无法递增变量

C++ Can't increment a variable

本文关键字:变量 C++      更新时间:2023-10-16

我正在为一个小型游戏引擎开发一个动画类,由于某种原因,帧计数器不想增加,它一直停留在 0 或 1。

这是动画::步骤代码(这是应该发生增量的地方):

void Animation::Step()
{
    ...
    time += (float)glfwGetTime();
    if (time >= Speed)
    {
        time = 0;
        if (f++ >= count - 1)
        {
            ...
        }
    }
    // Here I do some math to get a clip rectangle...
    ...
}

现在这是调用动画::步骤的部分:

inline void DrawAnimation(Animation ani, Vec2 pos, BlendMode mode, DrawTextureAttributes attr)
{
    ani.Step();
    ...
    // draw texture
}

在游戏主循环中:

void on_render(Renderer r)
{
    DrawTextureAttributes attr;
    attr.Scale = Vec2(1.5);
    r.DrawAnimation(ani, Vec2(320, 240), BlendMode::Normal, attr);
}

编辑:类定义:

class Animation
{
public:
    Animation() {}
    Animation(Texture2D tex, int rows, int cols, int *frames, float speed, bool loop=false);
    Animation(Texture2D tex, int rows, int cols, float speed, bool loop=false);
    Texture2D Texture;
    std::vector<int> Frames;
    float Speed;
    bool Loop;
    float getCellWidth();
    float getCellHeight();
    void Step();
    UVQuad RETUV;
private:
    int f, r, c; // here's F 
    float w, h;
    float time;
};

好吧,提前谢谢!(对不起,我的英语有点不好)

inline void DrawAnimation(Animation ani...

每次按值将对象传递给此函数时。因此,任何增量都将应用于此副本,而不是您的原始值。可以通过引用传递以获取所需的行为。

inline void DrawAnimation(Animation& ani...