In a 2D topdown space shooter, it is always classic to have multilevel parallax scrolling space backgrounds. Now, in libGDX I know this is possible, because there is a parallax scrolling demo, but this is without repeating the image. Is there a way, with the libGDX library, to create a multilevel parallax scrolling background?
Thanks, -cMp
Split your background image into some chunks and load them in queue then move them together like a train.
I understand the basic idea of parallax scrolling, however I was unsure of how to implement it using libGDX. Is it ok if I get some code?
My mind is not on this right now, but basically
stage.addAction(forever(delay(time, run(new Runnable(){
public void run(){
Building b = new Building();
stage.addActor(b);
b.addAction(sequence(moveTo(-b.getRight(), b.getY()), remove()));
}
}))));
You need to adjust “time” based on the width (or height if vertical scrolling) of building sprite.
I’m not a libgdx user but if you can get you camera values, you could just divide your camera position by an arbitrary value and apply that to your parallax layer.
Say camx/2.0f would make your parallax layer move at 1/2 the speed of your base layer.
ah ok, it was the actual doing it that confused me… I saw all of these advanced examples and got to the point of overthinking it. We all know how that gets with programming
Update con un mas question: I am now using this code for my scrolling:
package com.slyth.angrymasons.Screens;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class ParallaxLayer {
private TextureRegion region;
private float xRatio, yRatio;
public ParallaxLayer(TextureRegion region, float xRatio, float yRatio) {
super();
this.region = region;
this.xRatio = xRatio;
this.yRatio = yRatio;
}
public TextureRegion getRegion() {
return region;
}
public float getxRatio() {
return xRatio;
}
public float getyRatio() {
return yRatio;
}
public void render(float xPosition, float yPosition, float width, float height, SpriteBatch batch) {
batch.begin();
batch.draw(region, xPosition, yPosition, width, height);
batch.end();
}
}
package com.slyth.angrymasons.Screens;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ParallaxBackground {
private List<ParallaxLayer> layers;
private float width, height;
public ParallaxBackground(float width, float height) {
this.layers = new ArrayList<ParallaxLayer>();
this.width = width;
this.height = height;
}
public void render(float xPosition, float yPosition, SpriteBatch batch) {
for (ParallaxLayer layer : layers) {
float layerOffsetX = (xPosition * layer.getxRatio() % width);
float layerOffsetY = (yPosition * layer.getyRatio() % height);
layer.render(xPosition - width / 2f - layerOffsetX, yPosition - height / 2f - layerOffsetY, width, height, batch);
layer.render(xPosition - width / 2f - layerOffsetX + 5f, yPosition - height / 2f - layerOffsetY, width, height, batch);
}
}
public List<ParallaxLayer> getLayers() {
return layers;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public void addLayer(ParallaxLayer parallaxLayer) {
layers.add(parallaxLayer);
}
}
and if I send background the width and height of the screen (Gdx.graphics.getHeight()) then it draws the image large to the point of where I can see the individual pixels. If I use 10 and 10, it draws the image at a more reasonable scale, but certainly not 10 pixels. I would like to get this to the point of where the image is the size of the screen and the images repeat themselves. Any suggestions? Thanks, cMp
I’m not quite sure what’s happenning with your code but why is the width subtracted from your x-position?
Anyways, here’s a 2 layer paralkax bg example in java2d.
sorry I fixed the problem already. I should’ve said so, but yes this problem has been solved