Trying to make an "invincible" player state

Started by
1 comment, last by nano511 12 years, 8 months ago
I want the player to be invincible for a short time after he is hit. Right now, after he is hit he becomes invinsible forever.

here is the code


void Player::IsHit( int monsterXPos, Direction mosterdir )
{
if( !hurt && yPos >= (GROUNDYPOS - PLAYER_HEIGHT - 20) )
{
if( mosterdir == LEFT )
{
if( monsterXPos + 10 <= xPos + 30 )
{
invincibleTimer.Start();

health -= 1;
if( health < 0 )
health = 0;

xVel -= 70;
yVel -= 5;
}
}
else
{
if( monsterXPos + 40 >= xPos + 20)
{
invincibleTimer.Start();

health -= 1;
if( health < 0 )
health = 0;

xVel += 70;
yVel -= 5;
}
}
}

if( invincibleTimer.IsStarted() == true )
{
if( invincibleTimer.GetTicks() >= 10000 )
{
invincibleTimer.Stop();
hurt = false;
}
else
hurt = true;
}
}
Advertisement
Your going to have to put:


if( invincibleTimer.IsStarted() == true )
{
if( invincibleTimer.GetTicks() >= 10000 )
{
invincibleTimer.Stop();
hurt = false;
}
else
hurt = true;
}


in an update function or something. The reason why is that no time will pass (maybe half a millisecond or less), from where you start the timer to where that code above is called, since they are in the same function.
Oh thanks dude it works now!

This topic is closed to new replies.

Advertisement