I'm trying to reproduce this tutorial, but each time I try to implement it into my code, I always get a lot of errors. Now I finally managed to code something like this, the game runs, but nothing happens at all, my screen remains black without any texture drawn on it, and I just can't figure out what am I doing wrong here.
Here's my background class:

public class background
    public Vector2 position;
    public Texture2D texture;
    private List<Star> stars;
    private Random _Random;

    private int MaxX;
    private int MaxY;
    private int _Intensity;

    public background(ContentManager content, int Intensity)
        this.texture = content.Load<Texture2D>("star2");
        _Intensity = Intensity;
        this.MaxX = 1280;
        this.MaxY = 720;
        stars = new List<Star>();
        _Random = new Random(DateTime.Now.Millisecond);

        for (int i = 0; i < Intensity; i++)
            int StarColorR = _Random.Next(25, 100);
            int StarColorG = _Random.Next(10, 100);
            int StarColorB = _Random.Next(90, 150);
            int StarColorA = _Random.Next(10, 50);

            stars.Add(new Star(new Vector2(_Random.Next(MaxX / -2 - 1280, MaxX / 2 + 720), _Random.Next(MaxY / -2 - 1280, MaxY / 2 + 720)),
                new Color(StarColorR / 3, StarColorG / 3, StarColorB / 3, StarColorA / 3)));


    public void Update(GameTime gameTime)


    public void Draw(SpriteBatch spriteBatch)
        foreach (Star s in stars)
            spriteBatch.Draw(texture, s.Position, s.Color);


And the stars class:

class Star
    public Vector2 Position;
    public Color Color;
    public Star(Vector2 Position, Color Color)
        this.Position = Position;
        this.Color = Color;

As you can see I'm not using the exact same Star class as it is in the original code. First, I'm just trying to achieve the stars to appear on the game screen in random positions.


The code works as is when plugged in properly so...You must not have initialized background.

  1. Did you initialize the Background class in your Game1 class?

  2. Did you call Background's Draw method from the Game1 class?

Bonus 3. Your MaxX and MaxY fields tell me that your screen is 1280 X 720. Did you establish this in the Game1 class? (i.e. graphics.PreferredBackBufferWidth = 1280;)

Secret Bonus 4. You are telling your stars to form between -640 and 1360 for both X and Y values. Something more desirable might be:

 new Vector2(_Random.Next(0, MaxX), _Random.Next(0,MaxY))
| improve this answer | |
  • \$\begingroup\$ Well, I took a break, then I started the whole thing again from scratch. It turned out great, everything works fine, even the scrolling is okay now. Since you provided a couple useful informations which I'm using now to fine-tune my code, I'm going to accept your answer. Thanks a lot! \$\endgroup\$ – Laszlo Parazs Feb 15 '13 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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