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 attempting my first 2D vertical scroller and can't seem to get the background size to work. For some reason, it stays small. I think in the beginning of the parallax scrolling class, I was told to initialize variables and I know which ones I have to change.

public Texture2D picture; 
public Vector2 position = Vector2.Zero; 
public Vector2 offset = Vector2.Zero; 
public float depth = 0.0f; 
public float moveRate = 0.0f; 
public Vector2 pictureSize = Vector2.Zero; 
public Color color = Color.White; 

I think I must change pictureSize to make it bigger, as the current sizing I get this:

what the game looks like

Finally, how can I remove the whitespace from the ship image?


EDIT: Here is the code for the rendering of the actual ship:

public void Render(SpriteBatch batch)
{
    batch.Begin();
    batch.Draw(shipSprite, position, null, Color.White, 0.0f, spriteOrigin, 1.0f,
    SpriteEffects.None, 0.0f);
    batch.End();
}

The whitespace seems to be provided by XNA as I've tried clearing it using Photoshop. Because I can't directly upload to the site, I have put it here

This renders the background:

public void Draw()        
{           
layerList.Sort(CompareDepth);  
batch.Begin();  
for (int i = 0; i < layerList.Count; i++)       
{
   if (!moveLeftRight)
   {
       if (layerList[i].position.Y < windowSize.Y)
       {
           batch.Draw(layerList[i].picture, new Vector2(0.0f,       
           layerList[i].position.Y), layerList[i].color);
       }
       if (layerList[i].position.Y > 0.0f)
           batch.Draw(layerList[i].picture, new Vector2(0.0f,   
           layerList[i].position.Y - layerList[i].pictureSize.Y),  
           layerList[i].color);
       else
           batch.Draw(layerList[i].picture, new Vector2(0.0f, 
           layerList[i].position.Y + layerList[i].pictureSize.Y), 
           layerList[i].color);
   }
   else
   {
       if (layerList[i].position.X < windowSize.X)
       {
           batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X,
           0.0f), layerList[i].color);
       }
       if (layerList[i].position.X > 0.0f)
           batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X - 
           layerList[i].pictureSize.X, 0.0f), layerList[i].color);
       else
           batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X + 
       layerList[i].pictureSize.X, 0.0f), layerList[i].color);
       }                
   }             
   batch.End();       
}  

(It's from a XNA tutorial book I'm reading.)

share|improve this question
    
Edited for clarity. It feels like you don't fully understand what your code does and that makes it hard to both understand your question and to give an answer. Could you isolate the problem more clearly? –  Anko Jul 4 '13 at 12:52
add comment

1 Answer

up vote 0 down vote accepted

Microsoft provided a very easy to use class for parallaxing backgrounds that should take care of your problem. I changed it to scroll up to down for you (it originally scrolled right to left):

class ParallaxingBackground
    {
        // The image representing the parallaxing background
        Texture2D texture;

        // An array of positions of the parallaxing background
        Vector2[] positions;

        // The speed which the background is moving
        int speed;

        public void Initialize(ContentManager content, String texturePath, int screenHeight, int speed)
        {
            // Load the background texture we will be using
            texture = content.Load<Texture2D>(texturePath);

            // Set the speed of the background
            this.speed = speed;

            // If we divide the screen with the texture width then we can determine the number of tiles need.
            // We add 1 to it so that we won't have a gap in the tiling
            positions = new Vector2[screenHeight / texture.Height + 1];

            // Set the initial positions of the parallaxing background
            for (int i = 0; i < positions.Length; i++)
            {
                // We need the tiles to be side by side to create a tiling effect
                positions[i] = new Vector2(0, i * texture.Height);
            }
        }

        public void Update()
        {
            // Update the positions of the background
            for (int i = 0; i < positions.Length; i++)
            {
                // Update the position of the screen by adding the speed
                positions[i].Y += speed;
                // If the speed has the background moving to the left
                if (speed <= 0)
                {
                    // Check the texture is out of view then put that texture at the end of the screen
                    if (positions[i].Y <= -texture.Height)
                    {
                        positions[i].Y = texture.Height * (positions.Length - 1);
                    }
                }

                // If the speed has the background moving to the right
                else
                {
                    // Check if the texture is out of view then position it to the start of the screen
                    if (positions[i].Y >= texture.Height * (positions.Length - 1))
                    {
                        positions[i].Y = -texture.Height;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle viewportRectangle)
        {
            for (int i = 0; i < positions.Length; i++)
            {
                spriteBatch.Draw(texture, viewportRectangle, null, Color.White, 0.0f, positions[i], SpriteEffects.None, 0.0f);
            }
        }
    }

You can find the original here:

http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/drawing_the_background

I highly recommend going through the entirety of that tutorial if you're just getting started with XNA.

share|improve this answer
    
This works well thank you, i scrapped what I thought i knew about XNA because i was just confused and have started using that site to re learn it. being a beginning programmer a few concepts are still foreign to me but I'm getting there. Thanks! –  user2434321 Jul 8 '13 at 19:04
add comment

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.