首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >二维渲染颜色问题

二维渲染颜色问题
EN

Stack Overflow用户
提问于 2015-04-14 10:13:47
回答 1查看 49关注 0票数 0

我是2D编程的新手,现在我正在尝试制作一个2D游戏,里面有一些云彩在屏幕底部滚动。这是很容易做到的,但我不知道为什么,云被放在一个黑色的长方形。有什么解决办法吗?提前感谢!

http://gyazo.com/b16c9f3242c1ec62244f20bd1b61bba3

这里的代码:

代码语言:javascript
复制
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();

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-14 12:41:13

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29624709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档