简体   繁体   中英

How to parent texture on background in libgdx?

How can I put texture on my background image? In my top down game I have scrolling background image and an obstacles (ball,shoes,etc..). My problem is how can I attached those texture on my background image that scrolling down and those texture will follow the background.

Here is the screenshot of my game https://i.stack.imgur.com/jl03R.png

Codes

 // Load the sprite sheet as a texture
    cat = new Texture(Gdx.files.internal("cat.png"));
    catsprite = new Sprite(cat);
    player = new Rectangle();
    player.width = 20;
    player.height = 80;
    player.x = 300;
    player.y = 0;

    basketball = new Texture(Gdx.files.internal("equip/Basketball.png"));
    ball = new Circle();
    ball.x = 150;
    ball.y = 150;
    ball.radius = basketball.getWidth() / 4;
    ball.setPosition(350,500);

    shoes= new Texture(Gdx.files.internal("equip/Shoes.png"));
    sprite_shoes = new Sprite(shoes);
    rectShoes =  new Rectangle();
    rectShoes.setPosition(100,300);

      //background
    Background1 = new Texture(Gdx.files.internal("floor.png"));
    Background2 = Background1;
    yMax = -1270;
    yCoordBg1 = yMax*(-1);
    yCoordBg2 = 0;

Render

    camera.update();
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
    yCoordBg1+= BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime();
    yCoordBg2 = yCoordBg1 + yMax;  // We move the background, not the camera
    if (yCoordBg1 <= 0) {
        yCoordBg1 = yMax*(-1);
        yCoordBg2 = 0;
    }

    spriteBatch.draw(Background1, 0,yCoordBg1);
    spriteBatch.draw(Background2, 0,yCoordBg2);
    spriteBatch.draw(shoes,rectShoes.x,rectShoes.y);
    spriteBatch.draw(basketball, ball.x-ball.radius, ball.y-ball.radius);
    spriteBatch.draw(currentFrame,player.x, player.y);

It's probably easiest to move the player and the camera in stead of the background and all objects that should be tied to it. It makes it easier to understand and work with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM