List has no value after adding values in
Posted
by
Sigh-AniDe
on Game Development
See other posts from Game Development
or by Sigh-AniDe
Published on 2012-06-03T10:13:45Z
Indexed on
2012/06/03
10:48 UTC
Read the original article
Hit count: 227
I am creating a a ghost sprite that will mimic the main sprite after 10 seconds of the game. I am storing the users movements in a List<string>
and i am using a foreach
loop to run the movements. The problem is when i run through the game by adding breakpoints the movements are being added to the List<string>
but when the foreach
runs it shows that the list has nothing in it. Why does it do that? How can i fix it?
this is what i have:
public List<string> ghostMovements = new List<string>();
public void UpdateGhost(float scalingFactor, int[,] map)
{
// At this foreach, ghostMovements has nothing in it
foreach (string s in ghostMovements)
{
// current position of the ghost on the tiles
int mapX = (int)(ghostPostition.X / scalingFactor);
int mapY = (int)(ghostPostition.Y / scalingFactor);
if (s == "left")
{
switch (ghostDirection)
{
case ghostFacingUp:
angle = 1.6f;
ghostDirection = ghostFacingRight;
Program.form.direction = "";
break;
case ghostFacingRight:
angle = 3.15f;
ghostDirection = ghostFacingDown;
Program.form.direction = "";
break;
case ghostFacingDown:
angle = -1.6f;
ghostDirection = ghostFacingLeft;
Program.form.direction = "";
break;
case ghostFacingLeft:
angle = 0.0f;
ghostDirection = ghostFacingUp;
Program.form.direction = "";
break;
}
}
}
}
// The movement is captured here and added to the list
public void captureMovement()
{
ghostMovements.Add(Program.form.direction);
}
© Game Development or respective owner