我正在使用这个课程:
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循环中的ParallaxBackground类中执行此操作,但只重复三次。
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);
}
任何伪造的代码/代码都会有所帮助。
答案 0 :(得分:0)
您可以尝试这样的事情:
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);
}
}
你可能需要在drawLayers
函数中调整一些位置/宽度/高度值,但真正的魔法发生在drawParallaxLayer
- 应该能够保持不变。