gpt4 book ai didi

java - 2D渲染颜色问题

转载 作者:行者123 更新时间:2023-12-02 04:46:22 25 4
gpt4 key购买 nike

我是 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();

}

}

最佳答案

黑色矩形是因为您在 Sprite 批处理上调用了 disableBlending()。混合使得 Sprite 具有透明度。

关于java - 2D渲染颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com