Mild bug

It’s not a big deal but just figured I’d point out I think I spotted a bug in the code in this lesson for anyone following along. The library seems, as far as I can tell, to ‘wrap’ the srcRect values so I think it’s causing a repetition of the first frame.

Anyways, in code you have:
const int maxFrames{6};


if(frame > maxFrames) frame = 0;
// The above passes a frame value of 6. The max index should be 5.
// if(frame >= maxFrames) frame = 0; I believe would be correct here.

frame is then used to index the position in the sprite file with:
frame * (float) knight.width/6.f for the x of the srcRect (frame = 6 would be out of bounds of the file)
Again, the library seems to wrap the value to the image so the bug isn’t very apparent but these off by one errors can be a big problem for those starting out with code so I figured I’d point it out for the benefit of anyone following along. I’m so rusty with my coding right now I’m hoping I am not missing something;) heh.

I think that’s a good catch!

Further, I’ve heard that <= or >= are generally better options because they prevent bugs in situations you don’t expect to arise by blowing past a value you intended to hit.

Privacy & Terms