SDL向量下标超出范围



我得到一个'矢量下标超出范围'的错误。我知道这是由索引问题引起的,其中索引大于数组/集合的最大大小。然而,我不知道为什么它会到达那个阶段,因为我只在整个项目中一次将值增加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());
}

下面是错误的截图:

误差

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

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

最新更新