Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:47:12+00:00 2026-06-17T04:47:12+00:00

A few months ago when i was making 2D background scrolling game i needed

  • 0

A few months ago when i was making 2D background scrolling game i needed to make a background loop making it seem like the game character is moving , so i used the following algorithm to loop the background->

    int w=backgroundImage.getWidth();
     int h=backgroundImage.getHeight();
     int desX,desY;

     for(int x=0;x<w;x++)
     {
         for(int y=0;y<h;y++)
         {
             int px=backgroundImage.getRGB(x, y);

             desX=x-2;
             desY=y;


             if(desX<0)
             {
                 desX=w+desX-1;

             }

             backgroundImage.setRGB(desX, desY, px);
         }



     }

but after applying this algorithm to loop the whole background, i found that the whole game getting slow .

Is this algorithm any good or are there more better algorithms for looping the background??

Any code snippets would be highly appreciated.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:47:14+00:00Added an answer on June 17, 2026 at 4:47 am

    Depending on the language used this method could easily cause the performance drop you’re noticing. I suggest working with the draw offset of the background image instead of trying to change the raw pixel data itself.

    Firstly, make a background image wider than the game’s viewport. During each update cycle move the image by x pixels (x depends on the speed of movement you want to portray).

    Also, let’s assume you move the BG to the left. As you keep moving to the left, the right edge of the image will come into the viewport and you’ll notice a black band expanding from the right edge of the viewport. To prevent this artifact, you’ll need to be drawing TWO background images side-by-side.

    Also, to make it all look seamless you’ll have to obviously design to BG images to tile that way. As you might expect this method will be much faster than the existing snippet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used a tool a few months ago that scanned a specified website and
A few months ago I built some online samples like this one from Jeff
Quick personal background: I was hired a few months ago as the sole .NET
I made an iPhone game a few months ago, and am now trying to
I have a website that was launched a few months ago. I would like
I started writing a super-simple text adventure game a few months ago, and I
A few months ago I was thinking how to implement a closable event loop
A few months ago I came across service where you could make your code
I used 'package.skeleton()' to generate .Rd help files a few months ago. I have
A few months ago I used to be able to pre-fill the dialog box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.