分割后从Sprite表中删除背景

时间:2018-07-09 23:39:30

标签: libgdx

我正在使用libgdx来实现视差背景。现在,我需要在背景上显示一些动画对象。我下载了一个精灵表(带有白色背景上的对象的图像),并使用TextureRegion.split()方法将其拆分。但是,当我将对象绘制到屏幕上时,白色背景与对象一起显示,如下图所示:White background

我尝试使用batch.enableBlending(),但是没有用。有什么办法解决这个问题?还是应该仅使用具有透明背景的单个精灵进行编码?这是一个简短的代码片段:

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

        }

    }

这是我在上课时所说的地方:

    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(){}

    }

1 个答案:

答案 0 :(得分:1)

最好,最简单的方法是在游戏中使用图像之前使用图像编辑器使其背景透明。

我为此使用GIMP。