GameScreen显示黑屏 [英] GameScreen shows Black Screen

查看:54
本文介绍了GameScreen显示黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个游戏,必须避免小行星.我不得不处理一些混乱的坐标,而且我不得不猜测精灵的坐标.我现在有任意单位来描述我的世界.不幸的是,我的游戏屏幕无法正常工作.当我要渲染小行星时,游戏屏幕会显示黑屏.

I'm currently programming a game where you have to avoid asteroids. I had to deal with some messed up coordinates and I had to guess the coordinates for sprites. I now have arbitrary units that describe my world. Unfortunately, my game screen does not work fully. When I want to render my asteroids the game screen shows a black screen.

public class GameScreen extends Screen {

private OrthographicCamera cam;
private Spaceship spaceship;
private Asteroids asteroids;
private Background bg;



@Override
public void create() {
    // TODO Auto-generated method stub

    bg = new Background();
    spaceship = new Spaceship();
    asteroids = new Asteroids();

    float aspectratio = 16/10;

    cam = new OrthographicCamera(100, 100 * aspectratio);

    cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);

    cam.update();

    }



@Override
public void render(SpriteBatch batch) {
    // TODO Auto-generated method stub

    cam.update();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    bg.render(batch);
    spaceship.render(batch);
    batch.end();

}

上面显示的代码可以正常工作,并且向我展示了这一点:

The shown code above works just fine and shows me this:

当我在GameScreen类中为小行星添加渲染方法时,GameScreen只是黑色的:

When I add the render method for the asteroids in the GameScreen class the GameScreen is just black:

    @Override
    public void render(SpriteBatch batch) {
    // TODO Auto-generated method stub

    cam.update();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    bg.render(batch);
    spaceship.render(batch);
    batch.end();

    }

小行星类:

public class Asteroid {

private Vector2 p;
private Vector2 v;

private float mass;
private float radius;

public final float maxV = 200;
public final float minV = 50;

public final float rMin = 10;
public final float rMax = 30;

public final float minX = MyGdxGame.WIDTH;
public final float minY = MyGdxGame.HEIGHT;;

float alpha = MathUtils.random(0, 360);

public Asteroid(float maxX, float maxY, List<Asteroid> asteroids) {

    do {

    radius = MathUtils.random(rMin, rMax);
    masse = radius * radius * radius;

    float alpha = MathUtils.random(0, 360);

    p = new Vector2(MathUtils.random(minX, maxX), MathUtils.random(minY,
            maxY));

    float vBetrag = MathUtils.random(minV, maxV);

    v = new Vector2(vBetrag * MathUtils.cosDeg(alpha),
            vBetrag * MathUtils.cosDeg(alpha));
    } while (ueberlappMit(asteroids));

}

private boolean ueberlappMit(List<Asteroid> asteroids) {

    for(Asteroid a: asteroids){
        if(abstand(a) < radius + a.radius + 10){ //!
            return true;
        }
    }

    return false;

}

public void update(float deltaT, float xMin, float xMax, float yMin, float yMax) {
    p.x += v.x * deltaT;
    p.y += v.y * deltaT;

    while(p.x > xMax)
    {
        p.x -=  (xMax - xMin);  
    }
    while(p.x < xMin)
    {
        p.x += (xMax - xMin);
    }
    while(p.y > yMax)
    {
        p.y -= (yMax - yMin); 
    }
    while(p.y < yMin)
    {
        p.y += (yMax - yMin); 
    } 
}


public float abstand(Asteroid a2) {

    return p.dst(a2.p);

}

小行星分类:

public class Asteroids extends Entity {

private final int numberofAsteroids = 150;
private float xMin, xMax, yMin, yMax;
private List<Asteroid> asteroids = new ArrayList<Asteroid>();

private final int cyclicBoundaryConditionsMultiple = 2; 

public Asteroids() {


    xMin = MyGdxGame.WIDTH * (-cyclicBoundaryConditionsMultiple);
    xMax = MyGdxGame.WIDTH * (cyclicBoundaryConditionsMultiple);
    yMin = MyGdxGame.HEIGHT * (-cyclicBoundaryConditionsMultiple);
    yMax = MyGdxGame.HEIGHT * (cyclicBoundaryConditionsMultiple); 


    for (int i = 0; i < anzahl; i++) {
        Asteroid a = new Asteroid( xMax,  yMax, asteroids);
        asteroids.add(a);
    }

}

@Override
public void update() {

    for (Asteroid a : asteroids) {
        a.update(Gdx.graphics.getDeltaTime(), xMin, xMax, yMin, yMax);
    }

    for (int i = 0; i < numberofAsteroids; i++) {

        Asteroid a1 = asteroids.get(i);

        for (int j = i + 1; j < anzahl; j++) {

            Asteroid a2 = asteroids.get(j);

            float abstand = a1.abstand(a2);

            if (abstand < a1.getRadius() + a2.getRadius()) {

                calculateCollision(a1, a2);

            }

        }

    }

}


}

@Override
public void render(ShapeRenderer renderer) {

    for (Asteroid a : asteroids) {

        System.out.println("RENDER A");

        renderer.setColor(0, 0, 0, 1);
        renderer.circle(a.getP().x, a.getP().y, a.getRadius());

    }

    }

推荐答案

非常感谢您的帮助,伙计们,我解决了这个问题.方法private boolean ueberlappMit(List asteroids)检查小行星是否重叠以及是否应重新创建小行星.问题是,由于选择了太大的半径,游戏陷入了小行星类的do while循环中.

Thx alot for your help Guys, i solved the problem. The method private boolean ueberlappMit(List asteroids) checks if the asteroids overlap and if its the case the Asteroids shoud be created again. The Problem was that by selecting a too high radius the Game got stuck in the do while loop in the Asteroid class.

这篇关于GameScreen显示黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆