SDL 向量下标超出范围

SDL vector subscript out of range

我遇到 'vector subscript out of range' 错误。我知道这是由索引问题引起的,其中索引大于 array/collection 的最大大小。但是,我无法弄清楚为什么它会进入那个阶段,因为我只在整个项目中一次将值递增 1,如果它变得大于数组的大小,我将其重置为 0。这是关于 SDL 中动画的帧。有问题的索引变量是 m_currentFrame.

这里是动画精灵的'Process'方法,整个项目中只有这个地方调用了'm_currentFrame++',我ctrl+f搜索了一下:

    void
AnimatedSprite::Process(float deltaTime) {
    // If not paused...
    if (!m_paused){
        // Count the time elapsed.
        m_timeElapsed += deltaTime;
        // If the time elapsed is greater than the frame speed.
        if (m_timeElapsed > (float) m_frameSpeed){
            // Move to the next frame.
            m_currentFrame++;

            // Reset the time elapsed counter.
            m_timeElapsed = 0.0f;

            // If the current frame is greater than the number 
            //          of frame in this animation...
            if (m_currentFrame > frameCoordinates.size()){
                // Reset to the first frame.
                m_currentFrame = 0;

                // Stop the animation if it is not looping...
                if (!m_loop) {
                    m_paused = true;
                }

            }

        }
    }   
}

这是抛出错误的方法 (AnimatedSprite::Draw()):

    void
AnimatedSprite::Draw(BackBuffer& backbuffer) {      
    //          frame width
    int frameWidth = m_frameWidth;

    backbuffer.DrawAnimatedSprite(*this, frameCoordinates[m_currentFrame], m_frameWidth, m_frameHeight, this->GetTexture());
}

这是确切错误的屏幕截图:

error

if (m_currentFrame > frameCoordinates.size()){
    // Reset to the first frame.
    m_currentFrame = 0;

m_currentFrame == frameCoordinates.size() 时您已经需要重置,因为数组的最高索引是它的大小减一(从 0 开始计数)。