Aaron Macintyre
Aaron Macintyre

Reputation: 11

Help rendering sprite

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);
}

Upvotes: 0

Views: 232

Answers (3)

user269597
user269597

Reputation:

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 ;)

Upvotes: 1

Lee
Lee

Reputation: 144126

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.

Upvotes: 1

Anon.
Anon.

Reputation: 60008

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.

Upvotes: 3

Related Questions