tags:

views:

82

answers:

3

What i want the program to do is draw the graphic ground at each point in which a one appears in the .txt file, but everytime i run it, it doesnt draw the sprite?

here is the code.....

        using (StreamReader sr = new StreamReader("trainingLevel.txt"))
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                string[] elements = line.Split(',');
                foreach (string e in elements)
                {
                    int id = int.Parse(e);
                    if (id == 1)
                    {
                        CreatePlatform(x, y);
                    }
                    x += widthPlatform;

                }
                y += heightPlatform;
                x = 0;
            }
        }


    public void CreatePlatform(int x, int y)
    {

        groundSprite = new Sprite();
        groundSprite.Position = new Vector2(x, y);
        groundSprite.Load("Ground", Content, simulator);
        groundSprite.IsStatic = true;
        groundSprite.Geom.OnCollision += OnCollision;


    }



protected override void Draw(GameTime gameTime) {
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    List<Sprite> updateDraw = new List<Sprite>();
    foreach (Sprite z in updateDraw) {
        simulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
        z.Update(gameTime);
        spriteBatch.Draw(z.CurrentTexture, z.Position, null, Color.White, z.Rotation, z.Origin, 1, SpriteEffects.None, 1);
    }

    spriteBatch.End();

    base.Update(gameTime);
    base.Draw(gameTime);
}
+4  A: 

Here's the problem:

List<Sprite> updateDraw = new List<Sprite>();

updateDraw will, of course, contain zero elements at this point.

foreach (Sprite z in updateDraw)

Will then do absolutely nothing at all.

I am entirely unsure what you're actually trying to do there, though, so it's really up to you to figure that out and then fix it.

Anon.
what its suppose to do is there is a text file with 1s and 0s, for every one the program must render a graphic there
Aaron Macintyre
Then perhaps a good first step would be actually reading the text file?
Anon.
i have the code on a txt file if you would like a look at it, to understand it more?
Aaron Macintyre
Why don't you edit the relevant code into the original question?
Anon.
Not sure there is much more to understand. Whether you are reading a text file or not is not the issue since you are creating a new List<T> based on nothing. You should focus on populating the list in your code snippet with the data you get from reading the text file.
Cory Charlton
that should be it now
Aaron Macintyre
Let's work through this one step at a time. You read in the text file, and generate platforms based on its contents. What do you do with the platforms?
Anon.
the platforms are there to build the level
Aaron Macintyre
That's not what I asked. What do actually end up using them for in your code?
Anon.
sorry im struggling with xna, but i think they are to show the program where the ground.png file has to be rendered
Aaron Macintyre
Did you write this code?
Anon.
well it was my friends and i who wrote the code
Aaron Macintyre
Perhaps you should ask them for help understanding it?
Anon.
+1  A: 

Your foreach loop is enumerating an empty list - you probably want to make updateDraw a member of the class and add/remove items from it as the scene progresses rather than re-creating it each time it is drawn.

Lee
+1  A: 

So, you're creating a new, empty List of Sprites, iterating through this empty list and drawing all of the [nonexistent] objects in it, and yet nothing appears on the screen?

Perhaps you should try giving it something to draw.

EDIT: Looks like I've been beaten to it ;)

robinjam