Scrolling a 2D Background

New tutorial on scrolling a 2D background on my site XNADevelopment. This is the quick and dirty approach. It covers creating the project, adding images to the project and scrolling those images horizontally in a continuous loop.

I've posted the tutorial and the source code if you're interested. So far, I'm SUPER impressed with XNA development and just how easy it has been. The support from the XNA developers in the forums here has just been outstanding. These guys are really going over and above helping out all of us XNA newbies as we struggle to fly and learn the ropes.

I'm also just amazed at the variety and number of tutorials being put up each day by sites in the community. If you haven't been to these sites yet, I highly recommend them as you start to train yourself in XNA development.

The ZBuffer
exDream
XNASpot
LearnXNA
XBox 360 Homebrew

Game development really has just gotten just a whole heckuva lot more approachable.




Answer this question

Scrolling a 2D Background

  • Dick Campbell

    one more...

  • Constantin Dragomirov

    I've downloaded your little sample ... very nice ... but on my computer , the scroling is very very slow ... jerked

    What is the problem

    My computer have 1go ram, cpu 2gHz !!

    The graphic card is a ATI mobility radeon X300 ... it's not a dustbin :(

    is C# or XNA the problem



  • Christophe Vedel

    I've heard that the SpriteBatch is slow from other posts in the forums, but I have yet to experience this problem myself and I can't imagine that would be what he is experiencing at this point. (especially since my tutorial only draws 5 different sprites on the screen).

    It's not that the code runs slow for everyone, actually the backgrounds scroll along quite merrily for most people and computers, he however is reporting that everything runs slower on his computer.

    I can't imagine that SpriteBatch draws differently on one computer than another so I don't think that's the right place to be looking at for why this is happening for him.

    So far, I'm theorizing that it has something to do with his video card and the images. Either having issues with them not being in powers of 2 so certain things aren't optimized, or maybe that I continue to draw the images even when they aren't in the viewable area.




  • Dave Midgley

    My apologies Mark, I did indeed forget your site! I'll make sure to add it in and link to it from my site. Sorry about that.


  • rrowe007

    I've downloaded your little sample ... very nice ... but on my computer , the scroling is very very slow ... jerked

    What is the problem

    My computer have 1go ram, cpu 2gHz !!

    The graphic card is a ATI mobility radeon X300 ... it's not a dustbin :(

    is C# or XNA the problem



  • aruss

    I don't think that your code is bad ... because a friend coded similarly scrolling and the result is same .

    The scroll is very slow .... very bad ...

    In fact , all graphics samples, tutorials that i tried are very slow :((((

    Perhaps it's xna framework that is not optimized with my hardware ! I don't know ....

    but my computer can run some 3d FPS games with good performance ...



  • CarmenM

    Hi,

    I have not seen the code but check the following flag :

    graphics.AllowMultiSampling

    I find that when this is set to True, everything runs very slow. Try setting it to False and see what happens.

    Andy


  • that_guy

    Not a tutorial, but if anyone is interested in seeing some more code for scrolling a background:
    http://www.xnaexplorer.com/index.php/2006/09/04/arcadelib-and-the-moon-fighter-demo/

    The scrolling background is split into a reusable class that you just populate with a texture and tell how fast you want it to go. In the demo it's used to create a parallax scrolling effect where a set of foreground mountains move faster than a set of background mountains, creating depth.



  • MShetty

    Can you try what andy just said reebok and let us know if that makes a difference for you I'm very curious why you're getting such a different behavior in this tutorial. would be very good to know for future tutorials.


  • MF Recruit

    I'm not sure, but I noticed that the SpriteBatch class is quite slow and I think it is that, maybe you should try to draw the tiles with vertex and make a draw method by the hand to see if change something.
  • JTB1

    Also, I would like to point out since I just stumbled across it myself (doh!), Microsoft had already provided an awesome tutorial for scrolling a background.

    I'm going to have to compare it to the things I did to see what I could have done better. I'm not sure how I missed it, but I guess the great news is that XNA made it easy for me to do without even having to see Microsoft's tutorial.

    Anyway, to access the "How to: Make a Scrolling Background" by Microsoft for just another example of how you might go about doing it, just follow these steps:

    Click on the "Help" menu.
    Select "Contents"
    Expand "XNA"
    Expand "XNA Game Studio Express"
    Expand "Programming Guide"
    Expand "Graphics"
    Expand "2D Graphics"
    Select the Help topic, "How to: Make a Scrolling Background"


    Lol, next time I'll have to make sure I look through the Help contents a bit more extensively before I just duplicate what Microsoft has already given me. Well, I guess now the community has 2 samples of scrolling a background.






  • PublicError

    ok ... well ...

    I have run your project .exe on my computer ... the scroll was very slow

    then I have recompiled your project on my computer and i have executed your program via VS .... the scroll run correctly .... !!!

    I have executed the new .exe ... the scroll run correctly ...

    I understand nothing .. :(

    I must recompile project to run these correctly on my computer ! how bizarre !

    I have make the same operation with my friend code (which use the

    graphics.AllowMultiSampling = true;

    )

    .... idem ...

    someone have a explanation



  • Tomys

    I'm trying to write a 2D vertical scrolling background for a shooter. My implementation is a bit different. I have one BIG texture (1600X4194). Game screen resolution is 1024X768.

    I start by drawing a rectangle the size of the game window at the bottom of this texture and slowly move that rectangle up until it get to the top then I wrap. It works for the most part except I have one weird issue. Take a look at this line:
    th = mytextureinfo.Height / 2 - (screenheight/12);

    I would think I could just do:
    th = mytextureinfo.Height;

    But that doesn't work. At the start of the game I'm definitely drawing a rectangle outside the bounds of my texture. Why is that I'm using origin (0,0). I initialize y = mytextureinfo.Height - screenheight.

    Any ideas

    Here is the complete class:

    public class ScrollingBackground
    {
    public int speed = 1;
    private Texture2D mytexture;
    private TextureInformation mytextureinfo;
    private int screenheight, screenwidth;
    private Rectangle frame = new Rectangle();
    private Rectangle frame2 = new Rectangle();
    private Vector2 Pos = new Vector2(0f, 0f);
    int x, y;
    int tw, th;

    // ScrollingBackground.Load
    public void Load(GraphicsDevice Device, string TextureName)
    {
    mytextureinfo = Texture2D.GetTextureInformation(TextureName);
    mytexture = Texture2D.FromFile(Device, TextureName);
    screenheight = Device.Viewport.Height;
    screenwidth = Device.Viewport.Width;
    tw = mytextureinfo.Width;
    th = mytextureinfo.Height / 2 - (screenheight/12);
    Reset();
    }

    public void Reset()
    {
    x = (tw / 2) - (screenwidth / 2);
    y = th - screenheight ;
    frame.X = x;
    frame.Y = y;
    frame.Width = screenwidth;
    frame.Height = screenheight;

    frame2.X = x;
    frame2.Y = y;
    frame2.Width = screenwidth;
    frame2.Height = screenheight;

    }
    // ScrollingBackground.Update
    public void Update()
    {
    y -= speed;
    if (y < screenheight * -1)
    {
    Reset();
    return;
    }
    frame.Y = y;
    if (y < 0)
    {
    frame2.Y = th + y;
    frame2.Height = y * -1;
    }


    }
    // ScrollingBackground.Draw
    public void Draw(SpriteBatch Batch)
    {
    // Draw the first texture
    Batch.Draw(this.mytexture, Pos, this.frame, Color.White);
    if (y < 0)
    {
    //draw second texture
    Batch.Draw(this.mytexture, Pos, frame2, Color.White);
    }
    }
    }

  • VikasAgr

    Before I'd blame C# OR XNA, I'd blame my code. (I'm assuming you're talking about my tutorial and not the other one someone mentioned in this thread).

    Did you try tweaking the scrolling rate and seeing if playing with how far the images move in the X direction makes it go a little faster I've only seen how it ran on a couple computers, but every time it ran as expected for me, but I wouldn't doubt that I didn't do something right in the code and that's why on some machines it runs slower.

    If anyone knows what I did that's causing it to run very slow on certain PCs, please let me know so I can fix up the code and do things the right way.

    Hmm..could it be because the images aren't in powers of 2 Would that slow certain video cards down






  • Scrolling a 2D Background