Delaying a Foreach loop half a second
Posted
by
Sigh-AniDe
on Game Development
See other posts from Game Development
or by Sigh-AniDe
Published on 2012-06-05T07:36:39Z
Indexed on
2012/06/05
10:47 UTC
Read the original article
Hit count: 288
I have created a game that has a ghost that mimics the movement of the player after 10 seconds. The movements are stored in a list and i use a foreach
loop to go through the commands. The ghost mimics the movements but it does the movements way too fast, in split second from spawn time it catches up to my current movement. How do i slow down the foreach so that it only does a command every half a second? I don't know how else to do it.
Please help
this is what i tried : The foreach runs inside the update method
DateTime dt = DateTime.Now;
foreach ( string commandDirection in ghostMovements )
{
int mapX = ( int )( ghostPostition.X / scalingFactor );
int mapY = ( int )( ghostPostition.Y / scalingFactor );
// If the dt is the same as current time
if ( dt == DateTime.Now )
{
if ( commandDirection == "left" )
{
switch ( ghostDirection )
{
case ghostFacingUp:
angle = 1.6f;
ghostDirection = ghostFacingRight;
Program.form.direction = "";
dt.AddMilliseconds( 500 );// add half a second to dt
break;
case ghostFacingRight:
angle = 3.15f;
ghostDirection = ghostFacingDown;
Program.form.direction = "";
dt.AddMilliseconds( 500 );
break;
case ghostFacingDown:
angle = -1.6f;
ghostDirection = ghostFacingLeft;
Program.form.direction = "";
dt.AddMilliseconds( 500 );
break;
case ghostFacingLeft:
angle = 0.0f;
ghostDirection = ghostFacingUp;
Program.form.direction = "";
dt.AddMilliseconds( 500 );
break;
}
}
}
}
© Game Development or respective owner