0

So I am working on a side scrolling platformer. I changed some code to allow for scrolling backgrounds, which is working perfectly; however now when my levels advance the background stays the same.

This is the class for Layer

class Layer
{
    public Texture2D[] Textures { get; private set; }
    public float ScrollRate { get; private set; }

    public Layer(ContentManager content, string basePath, float scrollRate)
    {
        // Assumes each layer only has 3 segments.
        Textures = new Texture2D[3];
        for (int i = 0; i < 3; ++i)
            Textures[i] = content.Load<Texture2D>(basePath + "_" + i);

        ScrollRate = scrollRate;
    }

    public void Draw(SpriteBatch spriteBatch, float cameraPosition)
    {
        // Assume each segment is the same width.
        int segmentWidth = Textures[0].Width;

        // Calculate which segments to draw and how much to offset them.
        float x = cameraPosition * ScrollRate;
        int leftSegment = (int)Math.Floor(x / segmentWidth);
        int rightSegment = leftSegment + 1;
        x = (x / segmentWidth - leftSegment) * -segmentWidth;

        spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, 0.0f), Color.White);
        spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, 0.0f), Color.White);
    }
}

And this is level constructor in the loading part of class level

 public Level(IServiceProvider serviceProvider, Stream fileStream, int levelIndex)
    {
        // Create a new content manager to load content used just by this level.
        content = new ContentManager(serviceProvider, "Content");

        timeRemaining = TimeSpan.FromMinutes(2.0);

        LoadTiles(fileStream);

        // Load background layer textures. 
        layers = new Layer[3];

        layers[0] = new Layer(Content, "Backgrounds/Layer0", 0.2f);
        layers[1] = new Layer(Content, "Backgrounds/Layer1", 0.5f);
        layers[2] = new Layer(Content, "Backgrounds/Layer2", 0.8f);






        // Load sounds.
        exitReachedSound = Content.Load<SoundEffect>("Sounds/ExitReached");
    }

It looked like this before I changed it to allow for scrolling

    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(3);
  layers[i] = Content.Load<Texture2D>("Backgrounds/Layer" + i + "_" + segmentIndex);
}

Thank you for your time.

Pranaryx
  • 11
  • 2

2 Answers2

0

Ok, I note a couple of things:

  1. When you changed it to allow for scrolling, your layers went from one segment to 3.
  2. The image for each single-segment layer was chosen at random (out of three), now each layer has all three segments in order, as loaded by your for loop.
  3. You don't factor your level index in to the selection of your textures.

So, in the past, each level would give you a somewhat randomly selected choice of background, and now you get the same selection each time because you load segment 1, then 2, then 3 in your for loop.

Suggestions:

  • Re-introduce the randomness
  • Draw more backgrounds, and factor your level index in to the choice of backgrounds
guysherman
  • 581
  • 1
  • 8
  • 20
0

I didn't quite get what you wanted here:

spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, 0.0f), Color.White);
spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, 0.0f), Color.White);

More precisely, these two things:

leftSegment % Texture.Length
rightSegment % Textures.Length

Have you tried to draw inserting the Indexes manually? Like...

spriteBatch.Draw(Textures[0]...
spriteBatch.Draw(Textures[1]...
...

Because those leftSegment and rightSegment are dependent of the segmentWidth, right?

int segmentWidth = Textures[0].Width;

Is that width always going to be the same? If yes, there's no need to do this on the texture index on the draw method, because it'll always be the same indexes...

I didn't quite understand what you tried there(I'm sorry, I'm kinda dumb), but it just sounds... strange to me.. I don't know, maybe I'm just saying non-sense stuff...