2D渲染颜色问题

时间:2015-04-14 10:13:48

标签: java android libgdx

我是2D编程的新手,现在我正在尝试使用在屏幕底部滚动的一些云制作2D游戏。这很容易实现,但我不知道为什么,云被放在一个黑色的矩形中。有解决方案吗提前谢谢!

http://gyazo.com/b16c9f3242c1ec62244f20bd1b61bba3

代码在这里:

public class Scrollable {

    protected Vector2 position;
    protected Vector2 velocity;
    protected int width;
    protected int height;
    protected boolean isScrolledDown;

    public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
        position = new Vector2(x, y);
        velocity = new Vector2(0, scrollSpeed);
        this.width = width;
        this.height = height;
        isScrolledDown = false;
    }

    public void update(float delta) {
        position.add(velocity.cpy().scl(delta));

        if (position.y + height  < 0) {
            isScrolledDown = true;
        }
    }

    public void reset(float newY) {
        position.y = newY;
        isScrolledDown = false;
    }

    public boolean isScrolledDown() {
        return isScrolledDown;
    }

    public float getTailY() {
        return position.y + height;
    }

    public float getX() {
        return position.x;
    }

    public float getY() {
        return position.y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

}



public class ScrollHandler {

    private Clouds cloud1, cloud2, cloud3;
    public static final int SCROLL_SPEED = -59;

    public ScrollHandler(float yPos) {

        cloud1= new Clouds(80, 0, 30, 80, SCROLL_SPEED);
        cloud2 = new Clouds(50 , cloud1.getTailY(), 30, 80, SCROLL_SPEED);
        cloud3 = new Clouds(10, cloud2.getTailY(), 30, 80, SCROLL_SPEED);
    }

    public void update(float delta) {

        cloud1.update(delta);
        cloud2.update(delta);
        cloud3.update(delta);


         if (cloud1.isScrolledDown()) {
            cloud1.reset(cloud3.getTailY());

         } else if (cloud2.isScrolledDown()) {
            cloud2.reset(cloud1.getTailY());

         } else if (cloud3.isScrolledDown()) {
            cloud3.reset(cloud2.getTailY());
         }

}


    public Clouds getCloud1() {
        return cloud1;
    }

    public Clouds getCloud2() {
        return cloud2;
    }

    public Clouds getCloud3() {
        return cloud3;
    }

}



public class GameRenderer {

    private GameWorld myWorld;
    private OrthographicCamera cam;
    private ShapeRenderer shapeRenderer;

    private SpriteBatch batcher;

    private int midPointY;
    private int gameHeight;

    // Game Objects
    private Margarine marg;
    private ScrollHandler scroller;
    private Clouds cloud1, cloud2, cloud3;

    // Game Assets
    private TextureRegion cloud;

    public GameRenderer(GameWorld world, int gameHeight, int midPointY) {
        myWorld = world;

        this.gameHeight = gameHeight;
        this.midPointY = midPointY;

        cam = new OrthographicCamera();
        cam.setToOrtho(false, 136, gameHeight);

        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setProjectionMatrix(cam.combined);

        // Call helper methods to initialize instance variables
        initGameObjects();
        initAssets();
    }

    private void initGameObjects() {
        marg = myWorld.getMargarine();
        scroller = myWorld.getScroller();
        cloud1 = scroller.getCloud1();
        cloud2 = scroller.getCloud2();
        cloud3 = scroller.getCloud3();

    }

    private void initAssets() {

        cloud = AssetLoader.cloud;
    }

    public void render(float runTime) {

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        shapeRenderer.begin(ShapeType.Filled);

        // Draw Background color

        shapeRenderer.setColor(102 / 255.0f, 102 / 255.0f, 255 / 255.0f, 1);
        shapeRenderer.rect(0, 0, 136, midPointY + 66);
        shapeRenderer.rect(0, midPointY + 66, 136, 11);
        shapeRenderer.rect(0, midPointY + 77, 136, 52);

        shapeRenderer.end();

        batcher.begin();
        batcher.disableBlending();


        batcher.draw(cloud, cloud1.getX(), cloud1.getY(), cloud1.getWidth(),
                cloud1.getHeight());

        batcher.draw(cloud, cloud2.getX(), cloud2.getY(), cloud2.getWidth(),
                cloud2.getHeight());

        batcher.draw(cloud, cloud3.getX(), cloud3.getY(), cloud3.getWidth(),
                cloud3.getHeight());

        batcher.draw(AssetLoader.marg, marg.getX(), marg.getY(),
                marg.getWidth(), marg.getHeight());

        batcher.end();

    }

}

1 个答案:

答案 0 :(得分:2)

黑色矩形是因为你在sprite批处理上调用了disableBlending()。混合是允许精灵具有透明度的东西。