How to flash the hero every Y milisec?


(John Andersson) #1

I have a system where if the hero takes damage, an invincibility counter starts (it gets set to 0).

In my update part of the code, I have set it to

//Invincibility
if (staticInvincibilityCounter != staticInvincibilityCounterVal)
{
	//Only tint every x sec
		if (staticInvincibilityCounter / 3 == int)
		{
			spritemap.tintMode = 1;
			spritemap.color = 0x870E2D
		}else {
			spritemap.tintMode = 0;
			spritemap.color = 0xFFFFFF
		}
				
	staticInvincibilityCounter++;
}else {
	spritemap.tintMode = 0;
	spritemap.color = 0xFFFFFF
}

The part “staticInvincibilityCounter / 3 == int” is what is bugging me. I am trying to make it so that if the counter divided by 3 produces an int, it will tint the hero. This way, it will only tint it every 3 milisec, but I can’t seem to understand how to make it work…


(Justin Wolf) #2

You’d want to use a modulo, most likely. Try doing:

if (staticInvincibilityCounter % 3 == 0)

Modulos simply calculate a remainder between num1/num2. In this case: staticInvincibilityCounter / 3. If it returns 0, then there is no remainder and thus divisible by 3.


(Martí Angelats i Ribera) #3

why don’t you do:

//loop that blinks
if(staticInvincibilityCounter >= 3)
{
	staticInvincibilityCounter = 0;
	spritemap.tintMode = 1;
	spritemap.color = 0x870E2D
}
else
{
	spritemap.tintMode = 0;
	spritemap.color = 0xFFFFFF
}
staticInvincibilityCounter++;

This way you avoid the % operator wich consumes a lot of resources (relatively speaking).


(christopf) #4

but you will need an additional counter to check when it shall stop blinking. how much more resources does the % operator costs?


(Jacob Albano) #5

Not nearly enough to worry about.


(Ultima2876) #6

You could probably “only” do the operation with the % several thousand times per frame, as opposed to several tens of thousands. Basically, it makes NO practical difference.


(Martí Angelats i Ribera) #7

Yeah, you are right. I don’t know why i always want to make everything as efficient as i can.


(Jacob Albano) #8

Optimization is good. Premature optimization is the root of all evil.