How to draw scrolling background?

How do I draw a scrolling background?

  • How can I draw background tile in my 2D side-scrolling game? Is that loop logical for OpenGL es? My tile 2400x480. Also I want to use parallax scrolling for my game. batcher.beginBatch(Assets.background); for(int i=0; i<100; i++) batcher.drawSprite(0+2400*i, 240, 2400, 480, Assets.backgroundRegion); batcher.endBatch(); UPDATE And thats my onDrawFrame.I'm sending deltaTime for fps control. public void onDrawFrame(GL10 gl) { GLGameState state = null; synchronized(stateChanged) { state = this.state; } if(state == GLGameState.Running) { float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f; startTime = System.nanoTime(); screen.update(deltaTime); screen.present(deltaTime); } if(state == GLGameState.Paused) { screen.pause(); synchronized(stateChanged) { this.state = GLGameState.Idle; stateChanged.notifyAll(); } } if(state == GLGameState.Finished) { screen.pause(); screen.dispose(); synchronized(stateChanged) { this.state = GLGameState.Idle; stateChanged.notifyAll(); } } }

  • Answer:

    In this code, you are drawing the background 100 times in 100 different places in 1 frame. Learn how to properly use a game loop to run your game (here's a good article: http://www.koonsolo.com/news/dewitters-gameloop/). Your going to want to update the background's position in each update tick of your game loop and draw it at its current location in each frame. Post-Update Edit: So in screen.update(deltaTime); you'll want to change the current background position with something like: background.translate(BACKGROUND_SCROLL_SPEED * deltaTime, 0) Then, in screen.present(deltaTime); draw the background with: batcher.drawSprite(background.getX(), 240, 2400, 480, Assets.backgroundRegion); Let me know if you still have questions.

droidmachine at Game Development Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.