Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
share|improve this question
    
Where do you call captureMovement()? –  Kylotan Jun 3 '12 at 10:19
    
captureMovement() is being called in the turtleUpdate method after the user has given the movement he wants his sprite to move in. –  Sigh-AniDe Jun 3 '12 at 11:22
1  
Ok. You need to show that code, and show the object that contains UpdateGhost, captureMovement(), and whatever contains captureMovement(). It sounds like you're re-creating an object that should stay existing throughout. –  Kylotan Jun 3 '12 at 11:58
    
Ive added the code, please check edit –  Sigh-AniDe Jun 3 '12 at 12:31
1  
You're not showing where these objects and methods are declared. From what you've described it seems likely they are inside some other object which you're repeatedly creating, somehow. –  Kylotan Jun 3 '12 at 22:37
add comment

2 Answers

up vote 4 down vote accepted

You may have several problems:

A)

Using string for directions is not a good idea... because the string comparation is slow and case insensitive... is much better using an enumeration or integers.... I prefer enums...

   public enum Directions {None, Left, Right, Up, Down }

B)

Are sure you are calling to captureMovement()?

Are you clearing the list every frame? Before or After calling updateGhost()?

EDIT

C) Is the ghost instance you are using in the turtle class the same you are using in the game1 class?

D) I'm worried about the clearing of ghostmovements... where do you clear it?

E) I have redone your ghost code using enums and a data driven approach

public enum Direction { None, Left, Up, Right, Down }
struct RotData
{
    public float Angle;
    public Direction newDir;
    public RotData( float angle, Direction newdir ) { Angle = angle; newDir = newdir;  }
    public static readonly RotData Empty = new RotData( );
}

public class Ghost
{
   public List<Direction> ghostMovements = new List<Direction>( );

    Vector2 ghostPostition;
    Direction ghostDirection;
    float angle;

    RotData[][] rotationData = new RotData[][] 
    {
       new RotData[] { //Left 
            new  RotData(0, Direction.Up),    // Left
            new  RotData(MathHelper.PiOver2, Direction.Right),    // Up
            new  RotData(MathHelper.Pi, Direction.Down),          // Right
            new  RotData(-MathHelper.PiOver2, Direction.Left),    // Down
       }
       , new RotData[] {...} // Up
       , new RotData[] {...} // Right
       , new RotData[] {...} // Down

    };

    public void UpdateGhost( float scalingFactor, int[,] map )
    {
        // At this foreach, ghostMovements has nothing in it
        foreach ( Direction s in ghostMovements )
        {
            if ( s!=Direction.None && ghostDirection!= Direction.None )
            {
                RotData data = rotationData[( int ) s - 1][( int ) ghostDirection -1];
                angle = data.Angle;
                ghostDirection = data.newDir;
                Program.form.direction = Direction.None;
                break;
            }
        }
    }

This can be done easier if you always turn to right and/or the relation between angle and direction is fixed, then you only should use Directions and an array to convert directions to angles.

    public void UpdateGhost( float scalingFactor, int[,] map )
    {
        // At this foreach, ghostMovements has nothing in it
        foreach ( Direction s in ghostMovements )
        {
            if ( s!=Direction.None && ghostDirection!= Direction.None )
            {
                ghostDirection = (Directions)(((int)ghostDirection % 4)+1); // Turn right
                angle = DirectionsToAngles[(int)ghostDirection-1];
                Program.form.direction = Direction.None;
                break;
            }
        }
    }
share|improve this answer
    
Yes i am sure the captureMovement() is being called as the list has items and then when the update runs it has nothing in it anymore, Im not calling list.clear() anywhere in the application.Dont understand what is meant by "Before or After calling updateGhost()" –  Sigh-AniDe Jun 3 '12 at 11:24
    
I couldn't use Enums as the game is text based so I'm matching the text coming in with the direction it should move in –  Sigh-AniDe Jun 3 '12 at 11:26
    
with before or affter calling updateGohst I'm referring to hte clear() method of the list... –  Blau Jun 3 '12 at 19:43
    
you can use enums... you only have to parse them in your input handler to transform the text to known command... later is easier and faster working with enums than with strings... –  Blau Jun 3 '12 at 19:45
    
The ghost instance i was using was not the same that was used in the game class. Noob mistake. Thanks Blau –  Sigh-AniDe Jun 4 '12 at 12:19
add comment

Well could you show the constructor for the class you're using ? Also i would generally
initialize a List ghostMovements in the constructor ,because it's less error prone also the garbage collector might do some weird things sometimes.And the issue might actually lie there ,but this is just a hunch.

share|improve this answer
    
I didn't create a constructor for the class –  Sigh-AniDe Jun 3 '12 at 12:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.