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

  • SEARCH
  • Home
  • 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 9184981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:11:05+00:00 2026-06-17T19:11:05+00:00

I have been looking for 10 hours (literally) and I’m done, I need to

  • 0

enter image description here

I have been looking for 10 hours (literally) and I’m done, I need to ask. Thing is I’m learning How use LibGdx to program Java games. I’m doing a Horizontal Space Ship Game. So, my worst problem here is that I do not know how do scroll (I think draw will explain better). I want to draw a huge background (Space) and make my OrthographicCamera move right like with my SpaceShip, so it will create a Scroll effect with the Space Background. No enemies and nothing but the ship on the screen.

I’m trying this:

 public void moveCamera(float x,float y){
    cam.position.set(x, y, 0);
 }

Then I use that method in my WorldRender render() method:

public void render(float delta){ 
    ship.Move(delta);
    moveCamera(ship.getPosition().x,ship.getPosition().y);
    cam.update();
    System.out.println(""+cam.position);
    spriteBatch.begin();
        drawBackground();
        drawShip();
    spriteBatch.end();
}

I actually move the camera position (I can see that thanks to the println), but It isn’t moving in the game, so SpaceShip just disappears by the edge of the window.

I also tried this before spriteBatch.end()

spriteBatch.setProjectionMatrix(camera.combined);

but when I do that windows only shows a black screen, no ship, no nothing.
As I said, I’m desperate, I see lot of examples (scroll with mouse, paralexscrolling etc) but all are to advanced or just nothing to do with my code.

This is how I draw stuff. Background and ship are textures inside WorldRender. I draw background image very wide, so my intention is do some scrolling over as I said. That’s the code

private void loadTextures(){
    shipTexture=new Texture(Gdx.files.internal("nave.png"));
    background=new Texture(Gdx.files.internal("fondo.jpg"));
}

public void drawShip(){
    spriteBatch.draw(shipTexture,ship.getPosition().x*ppuX,ship.getPosition().y*ppuY, ship.WIDTH*ppuX,ship.HEIGHT*ppuY);
}

public void drawBackground(){
    spriteBatch.draw(background, -10*ppuX,0*ppuY, Gdx.graphics.getWidth()*10,Gdx.graphics.getHeight());     
}

Here you can download the code if someone want to help in hardcore mode

My code (not working)

I FINALLY SOLVED IT!

That’s the code I used in a class name WorldRenderer, which have methods that are called within GameScreen for render, resize etc

 public WorldRenderer(World world) {
    // TODO Auto-generated constructor stub
    
    this.world=world;
    this.ship=world.getShip();
    this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
    
    this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
    this.cam.position.set(ship.getPosition().x,CAMERA_HEIGHT/2,0);
    this.cam.update();//actualizamos la camara
    spriteBatch=new SpriteBatch();
    loadTextures();
}

private void loadTextures(){
    shipTexture=new Texture(Gdx.files.internal("nave.png"));
    background=new Texture(Gdx.files.internal("fondo.jpg"));
}

  public void drawShip(){
          spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y,10,10);
}

public void drawBackground(){
    spriteBatch.draw(background, 0,0, 500,50);
} 

 public void render(float delta){ 
    ship.Move(delta);
    moveCamera(ship.getPosition().x);
    spriteBatch.setProjectionMatrix(cam.combined); 
    spriteBatch.begin();
        drawBackground();
        drawShip();
    spriteBatch.end();
}

 public void moveCemara(float x){
    cam.position.set(x+20,cam.position.y, 0);
    cam.update();
}

Inside the Ship I have this method which I call within render in WorldRenderer to move It

public void Move(float delta){
    if(Gdx.input.isKeyPressed(Keys.LEFT)) this.position.x -=velocity *delta;
    if(Gdx.input.isKeyPressed(Keys.RIGHT)) this.position.x +=velocity *delta;
    if(Gdx.input.isKeyPressed(Keys.UP)) this.position.y +=velocity *delta;
    if(Gdx.input.isKeyPressed(Keys.DOWN)) this.position.y -=velocity *delta;
}

Also I want to thanks very much to the people who helped me. I’m marking first answer as the good one, but, mix both was what gave me the real solution.

I leave here some tutorials I followed which are pretty good for noobs

That’s a good everything-from-scratching-tutorial
LiGdxForNoobs

A simple platform game
platformGame

A very simple game
bucketGame

  • 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-17T19:11:07+00:00Added an answer on June 17, 2026 at 7:11 pm

    Its not clear how your drawing? I’m not sure if your doing this approach correctly.. Can you provide details of your background and ship? Can you provide details on you background image, is it a huge image that your scrolling around or is it a repeated image you want to repeat as you scroll?

    –EDIT–

    ok i think i have an idea what might be up. I would normally apply the camera to the current context.

    Place the following in your resize

        public void resize(int width, int height) {
        cam = new OrthographicCamera(width, height);
        cam.translate(width / 2, height / 2, 0);
    }
    

    Place the following in the start of your render()

    cam.position.set(posX,posY,0);
    cam.update();
    cam.apply(Gdx.gl10);
    Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // #14
    

    This will make you have a clear screen with the origin set at the bottom left of the window. You should then draw your background first

    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();
    spriteBatch.draw(background,0,0,sizeX,sizeY);
    spriteBatch.end()
    

    see how that looks as you move your camera position posX and posY. Then add your ship to the mix

    — MORE EDITS —

    you can then calculate the posX and posY as

    posX = defaultOffsetX+shipX

    and so on..

    Anyhow hope this helps
    I’m still only learning myself so this might not be the best method.. but it seems to work.

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

Sidebar

Related Questions

I am learning OpenXML. I have been looking for hours trying to find how
I have been googling for hours looking for something to handle my situation. I
I have been looking for hours on how to connect to my local SQL
I have been looking at this for hours and can't figure this out. If
I have been looking at that same code for two hours now and I
Hello internet, I have been looking for the past two hours for a script
I have been looking around for hours on how to do this, I think
I am about to explode. I have been looking for two hours for a
I have been looking at this for hours but can't find out what's wrong.
I have been looking at this for quite few hours and I don't think

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.