I am using libgdx to implement a Parallax background. Now I need to show some animated object over the background. I downloaded a sprite sheet(with images of objects on white background), and split it using TextureRegion.split() method. But when I draw the objects to screen, white background is shown along with the object as in this image:
I tried using batch.enableBlending() but it didn't work. Is there any way to resolve this issue? Or should I code only using individual sprites with transparent background? Here is a brief code snippet :
public class Animator extends Actor{
int FRAME_COLS = 3, FRAME_ROWS = 3;
Animation<TextureRegion> eagleAnimation;
Texture eagleSheet;
float stateTime;
Animator(Texture sheet) {
eagleSheet = sheet;
TextureRegion[][] tmp = TextureRegion.split(eagleSheet, eagleSheet.getWidth()/FRAME_COLS,
eagleSheet.getHeight()/FRAME_ROWS);
TextureRegion[] eagleFrames = new TextureRegion[FRAME_COLS*FRAME_ROWS];
int index = 0;
for(int i = 0; i < FRAME_ROWS; i++) {
for(int j = 0; j < FRAME_COLS; j++) {
eagleFrames[index++] = tmp[i][j];
}
}
eagleAnimation = new Animation<TextureRegion>(0.09f, eagleFrames);
stateTime = 0f;
}
@Override
public void draw(Batch batch, float parentAlpha) {
stateTime += Gdx.graphics.getDeltaTime();
TextureRegion currentFrame = eagleAnimation.getKeyFrame(stateTime, true);
batch.enableBlending(); // doesn't work
batch.draw(currentFrame, 250, 250, 264, 264);
}
}
This is where I call above class from :
public class GameScreen implements Screen {
MyGdxGame game;
Texture eagleSheet;
OrthographicCamera camera;
Array<Texture> layers;
Stage stage;
public GameScreen(MyGdxGame game) {
this.game = game;
stage = new Stage(new ScreenViewport());
camera = (OrthographicCamera) stage.getViewport().getCamera();
layers = new Array<Texture>();
for(int i = 1 ; i <= 6; i++) {
layers.add(new Texture(Gdx.files.internal("parallax/img" + i + ".png")));
}
ParallaxBackground parallaxBackground = new ParallaxBackground(layers);
parallaxBackground.setSpeed(1);
parallaxBackground.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.addActor(parallaxBackground);
eagleSheet = new Texture(Gdx.files.internal("eagle.png"));
Animator animator = new Animator(eagleSheet);
animator.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.addActor(animator);
}
@Override
public void render (float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void dispose () {
stage.dispose();
}
@Override
public void show() {}
@Override
public void hide(){}
@Override
public void resume(){}
@Override
public void resize(int x, int y){}
@Override
public void pause(){}
}