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
Related Q & A:
- How do I add a background image to my JFrame?Best solution by Stack Overflow
- How do I put a background on my YouTube?Best solution by Yahoo! Answers
- How do I add a custom background image to my Tumblr?Best solution by Yahoo! Answers
- How do i put a background on GIMP 2.6?Best solution by Yahoo! Answers
- How do I make a background image full page with CSS?Best solution by stradegyadvertising.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.