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.

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();
        }            
    }
}   
share|improve this question
2  
Are you not getting the results you want with your current code? What have you tried already? What about it isn't working? –  Byte56 Apr 10 '12 at 23:50
    
@Byte56 i think thats not logical.Because i draw background already for 2400*100 pixels.I'm searching for better idea. –  droidmachine Apr 11 '12 at 7:00
    
In android sample application Lunar Lader they have explain how to move background of a game. Using that sample, this article explain this concept very well –  TofeeqAhmad Dec 26 '12 at 5:15
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
i'm using a different game loop already in my upper class.Are there any other solution for this? –  droidmachine Apr 11 '12 at 7:03
    
What does your game loop look like? Because you need to be changing the position in the update section and drawing it in its current position in the draw section. This is the best (and pretty much only) way to make real-time games. If you edit your question to show what your game loop looks like, I will help you understand how to do this. –  Amplify91 Apr 11 '12 at 10:00
    
I have updated my question. –  droidmachine Apr 11 '12 at 12:19
    
@droidmachine I updated my answer. –  Amplify91 Apr 12 '12 at 2:12
    
I have used this but no solution: in update(): backx+=200*deltaTime; in present(): batcher.drawSprite(backx, 240, 2400, 480, Assets.backgroundRegion); –  droidmachine Apr 12 '12 at 15:32
show 6 more comments

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.