Java 如何在Libgdx中无限重复背景?

Java 如何在Libgdx中无限重复背景?,java,android,libgdx,infinite-scroll,Java,Android,Libgdx,Infinite Scroll,我正在使用这个类: import com.badlogic.gdx.graphics.g2d.TextureRegion; public class ParallaxLayer { // the Texture sitting on this layer public TextureRegion region; /** * how much shall this layer (in percent) be moved if the whole background is moved

我正在使用这个类:

import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class ParallaxLayer {

//  the Texture sitting on this layer

public TextureRegion region;

/**
 * how much shall this layer (in percent) be moved if the whole background is moved
 * 0.5f is half as fast as the speed
 * 2.0f is twice the speed
 */
float ratioX, ratioY;

/**
 * current position
 */
float positionX, positionY;

/**
 * 
 * @param pRegion
 * @param pRatioX
 * @param pRatioY
 */
public ParallaxLayer(TextureRegion pRegion, float pRatioX, float pRatioY) {
    region = pRegion;
    ratioX = pRatioX;
    ratioY = pRatioY;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveX(float pDelta) {
    positionX += pDelta * ratioX;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveY(float pDelta) {
    positionY += pDelta * ratioY;
}
   import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class ParallaxLayer {
/**
 * the Texture sitting on this layer
 */
public TextureRegion region;

/**
 * how much shall this layer (in percent) be moved if the whole background is moved
 * 0.5f is half as fast as the speed
 * 2.0f is twice the speed
 */
float ratioX, ratioY;

/**
 * current position
 */
float positionX, positionY;

/**
 * 
 * @param pRegion
 * @param pRatioX
 * @param pRatioY
 */
public ParallaxLayer(TextureRegion pRegion, float pRatioX, float pRatioY) {
    region = pRegion;
    ratioX = pRatioX;
    ratioY = pRatioY;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveX(float pDelta) {
    positionX += pDelta * ratioX;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveY(float pDelta) {
    positionY += pDelta * ratioY;
}
}

这门课:

import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class ParallaxLayer {

//  the Texture sitting on this layer

public TextureRegion region;

/**
 * how much shall this layer (in percent) be moved if the whole background is moved
 * 0.5f is half as fast as the speed
 * 2.0f is twice the speed
 */
float ratioX, ratioY;

/**
 * current position
 */
float positionX, positionY;

/**
 * 
 * @param pRegion
 * @param pRatioX
 * @param pRatioY
 */
public ParallaxLayer(TextureRegion pRegion, float pRatioX, float pRatioY) {
    region = pRegion;
    ratioX = pRatioX;
    ratioY = pRatioY;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveX(float pDelta) {
    positionX += pDelta * ratioX;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveY(float pDelta) {
    positionY += pDelta * ratioY;
}
   import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class ParallaxLayer {
/**
 * the Texture sitting on this layer
 */
public TextureRegion region;

/**
 * how much shall this layer (in percent) be moved if the whole background is moved
 * 0.5f is half as fast as the speed
 * 2.0f is twice the speed
 */
float ratioX, ratioY;

/**
 * current position
 */
float positionX, positionY;

/**
 * 
 * @param pRegion
 * @param pRatioX
 * @param pRatioY
 */
public ParallaxLayer(TextureRegion pRegion, float pRatioX, float pRatioY) {
    region = pRegion;
    ratioX = pRatioX;
    ratioY = pRatioY;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveX(float pDelta) {
    positionX += pDelta * ratioX;
}

/**
 * move this layer
 * @param pDelta
 */
protected void moveY(float pDelta) {
    positionY += pDelta * ratioY;
}
}

在主要课程中:

 camera=new OrthographicCamera(400,240);
camera.position.x=200;
camera.position.y=120;
camera.update();
batch=new SpriteBatch();

layer1=atlas.findRegion("layer1");
layer2=atlas.findRegion("layer2");
layer3=atlas.findRegion("layer3");
ParallaxLayer l1=new ParallaxLayer(layer1,0,0);
ParallaxLayer l2=new ParallaxLayer(layer2,0.5f,0);
ParallaxLayer l3=new ParallaxLayer(layer3,1,0);
ParallaxLayer[] layers={l1,l2,l3};
background=new ParallaxBackground(layers, camera,batch);

// [...] in render
background.moveX(30*delta); // move to the right to show the effect

background.render();
实现视差滚动效果,但我想无限滚动,但无法得到它。我试着在视差背景类的for-loop下这样做,但只重复了三次

  posXbg1L1 = layer.positionX;

        posXbg2L1 = posXbg1L1 - layer.region.getRegionWidth();


       if (camera.position.x <= posXbg2L1 - camera.viewportWidth / 2) {
           // Gdx.app.log("TAG", camera.position.x + ":" + posXbg2L1 + camera.viewportWidth / 2);
            posXbg1L1 = posXbg2L1;

        }

        batch.draw(layer.region, -camera.viewportWidth / 2
                - posXbg1L1, -camera.viewportHeight / 2
                - layer.positionY);

        batch.draw(layer.region, -camera.viewportWidth / 2
                - posXbg2L1, -camera.viewportHeight / 2
                - layer.positionY);

    }
posXbg1L1=layer.positionX;
posXbg2L1=posXbg1L1-layer.region.getRegionWidth();

如果(camera.position.x您可以尝试以下方法:

TextureRegion[] backgrounds = [...your array of background textures...];
float[] parallax = {...your parallax coefficients...}; //For example {0.2f, 0.1f}

public void drawLayers(Batch batch, OrthographicCamera camera) {
    batch.setColor(Color.WHITE);
    for(int b = backgrounds.length - 1; b >= 0; b--) {
        TextureRegion background = backgrounds[b];

        if(background != null) {
            float x = (camera.position.x - camera.viewportWidth / 2f * camera.zoom);
            float y = camera.position.y - camera.viewportHeight / 2f * camera.zoom + camera.viewportHeight / 15f * camera.zoom;

            float rWidth = camera.viewportWidth * 1.5f * camera.zoom;
            float rHeight = (rWidth / background.getRegionWidth()) * background.getRegionHeight();

            drawParallaxLayer(batch, background, parallax[b], x, y, rWidth, rHeight);
        }
    }
}

public static void drawParallaxLayer(Batch batch, TextureRegion region, float parallax, float x, float y, float width, float height) {
    for(int j = 0; j < 3; j++) {
        batch.draw(region, x + (j * width) - ((x * parallax) % width) - (width / 2f), y, width, height);
    }
}
TextureRegion[]backgrounds=[…您的背景纹理数组…];
float[]视差={…您的视差系数…};//例如{0.2f,0.1f}
公共空心图层(批处理、正交摄影机){
batch.setColor(颜色为白色);
对于(int b=backgrounds.length-1;b>=0;b--){
纹理区域背景=背景[b];
如果(背景!=null){
float x=(camera.position.x-camera.viewportWidth/2f*camera.zoom);
float y=camera.position.y-camera.viewportHeight/2f*camera.zoom+camera.viewportHeight/15f*camera.zoom;
float rWidth=camera.viewportWidth*1.5f*camera.zoom;
float rHeight=(rWidth/background.getRegionWidth())*background.getRegionHeight();
drawParallaxLayer(批次、背景、视差[b]、x、y、rWidth、rHeight);
}
}
}
公共静态空隙绘制视差层(批次、纹理区域、浮动视差、浮动x、浮动y、浮动宽度、浮动高度){
对于(int j=0;j<3;j++){
批量绘制(区域,x+(j*宽度)-(x*视差)%width)-(宽度/2f),y,宽度,高度);
}
}
您可能需要在
drawLayers
函数中调整一些位置/宽度/高度值,但真正的神奇发生在
drawParallaxLayer
-它应该能够保持不变。

这可能会有所帮助