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);
}
EDIT:
// Ghost is the name of the class that creates the ghost sprite
// as well as where captureMovement() is in.
Ghost ghost = new Ghost();
// This update is in a class named turtle
public void Update(float scalingFactor, int[,] map)
{
// current position of the turtle on the tiles
int mapX = (int)(turtlePosition.X / scalingFactor);
int mapY = (int)(turtlePosition.Y / scalingFactor);
//The rest of the code that contains the captureMovement method being called
//looks the same as the if statement with just different directions.
if (Program.form.direction == "left")
{
ghost.captureMovement();
switch (turtleDirection)
{
case turtleFacingUp:
angle = 1.6f;
turtleDirection = turtleFacingRight;
Program.form.direction = "";
break;
case turtleFacingRight:
angle = 3.15f;
turtleDirection = turtleFacingDown;
Program.form.direction = "";
break;
case turtleFacingDown:
angle = -1.6f;
turtleDirection = turtleFacingLeft;
Program.form.direction = "";
break;
case turtleFacingLeft:
angle = 0.0f;
turtleDirection = turtleFacingUp;
Program.form.direction = "";
break;
}
}
}
// This update is in the game1 class (main class)
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
turtle.Update(scalingFactor, map);
ghost.UpdateGhost(scalingFactor, map);
base.Update(gameTime);
}