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'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)
    {
        spriteBatch.Begin();
        foreach (Star s in stars)
        {
            spriteBatch.Draw(texture, s.Position, s.Color);
        }
        spriteBatch.End();
    }


}

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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))
share|improve this answer
    
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! –  Laszlo Parazs Feb 15 '13 at 17:54

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.