如何在libgdx中的父母纹理背景?

时间:2017-04-04 10:31:22

标签: java android libgdx

如何在背景图像上放置纹理?在我自上而下的游戏中,我有滚动背景图像和障碍物(球,鞋等)。我的问题是如何将这些纹理附加到我的背景图像上,向下滚动,这些纹理将跟随背景。

以下是我的游戏https://i.stack.imgur.com/jl03R.png

的屏幕截图

代码

 // 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;

渲染

    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);

1 个答案:

答案 0 :(得分:0)

最简单的方法是移动播放器和相机而不是背景和所有应该绑在它上面的物体。它使人们更容易理解和使用。