XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

Changing backgrounds help.

Last post 05/11/2009 7:46 by AlectoPerfecto. 1 replies.
  • 05/11/2009 5:09

    Changing backgrounds help.

    Hello (again).

    For my platformer game I'm making, (using the Platformer Starter Kit), I am trying to make it so each level has a different background.

    I am a complete newb and have only started using Xna recently.



    Is this the code I need to change?


    public

     

    Level(IServiceProvider serviceProvider, string path)

     

    {

     

    // Create a new content manager to load content used just by this level.

     

    content =

    new ContentManager(serviceProvider, "Content");

     

    timeRemaining =

    TimeSpan.FromMinutes(1.5);

     

    LoadTiles(path);

     

    // Load background layer textures. For now, all levels must

     

     

    // use the same backgrounds and only use the left-most part of them.

     

    layers =

    new Texture2D[3];

     

     

    for (int i = 0; i < layers.Length; ++i)

     

    {

     

    // Choose a random segment if each background layer for level variety.

     

     

    int segmentIndex = random.Next(1);

     

    layers[i] = Content.Load<

    Texture2D>("Backgrounds/Layer" + i + "_" + segmentIndex);

     

    }

    }


    Any help will be gratefully accepted.

  • 05/11/2009 7:46 In reply to

    Re: Changing backgrounds help.

    I have not looked at the Platform example but changing your background is a very simple procedure.

    Your backgrounds are sprite images. They are loaded by adding them to your project and then making Texture2D objects.

                vpWidth = graphics.PreferredBackBufferWidth;
                vpHeight = graphics.PreferredBackBufferHeight;
                List<Texture2D> backgrounds = new List<Texture2D>();
                backgrounds.Add(new Texture2D(GraphicsDevice, vpWidth, vpHeight);
                backgrounds[0] = Content.Load<Texture2D>("backgroundScreen");
    backgrounds[0] = Content.Load<Texture2D>("backgroundScreen1");
    backgrounds[0] = Content.Load<Texture2D>("backgroundScreen2");
    backgrounds[0] = Content.Load<Texture2D>("backgroundScreen3");
    backgrounds[0] = Content.Load<Texture2D>("backgroundScreen4");

    Then in your draw method
    spriteBatch.Begin();
    if(level == 1)
                    spriteBatch.Draw(backgrounds[0], Vector2.Zero, Color.White);
    if(level == 2)
                    spriteBatch.Draw(backgrounds[1], Vector2.Zero, Color.White);
    if(level == 3)
                    spriteBatch.Draw(backgrounds[2], Vector2.Zero, Color.White);

    spriteBatch.End();
Page 1 of 1 (2 items) Previous Next