Sign in to follow this  
Programmer16

Why does this code work?

Recommended Posts

Programmer16    2321
static unsigned int	nMod	= 1;

if(!m_bActive)
	return;

if(m_Object.m_fTime >= m_Object.m_fDelay)
{
	m_Object.m_fTime = 0.0f;
	m_nCurrentFrame += nMod;
	if(m_nCurrentFrame == m_nFrameCount)
	{
		if(!m_bRepeat)
			Activate(false);
		else
			nMod = -1;
	}
	if(m_nCurrentFrame == 0)
		nMod = 1;
}

Why does this code work perfectly? Should "nMod = -1;" set it to some huge number (I don't know it off hand.) It worked perfectly, but IIRC it shouldn't. I was just wondering. Thanks!

Share this post


Link to post
Share on other sites
dragongame    538
Because you generate a carry bit.
Or more clear:

lets say you work modolo 10
then -1 mod 10 = 9;

so you can subtract with -1.

lets say you want (5 - 1) mod 10
then you could also write
(5 + 9) = 14
14 mod 10 = 4 which is the same as (5 - 1) = 4 [mod 10]

hope this helps

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this