我正在开发一个项目,用户必须使用Spaceship(Sprite)避免使用Asteroids(使用ShapeRenderer绘制)。小行星保存在列表数组中。现在我正试图在Sapceship和Asteroids碰撞时创建碰撞方法。为了使它尽可能简单,我想在宇宙飞船周围绘制一个圆圈并检查碰撞。 Eclipse说我有一个java.lang.IllegalStateException: begin must be called first
。
小行星类:
public Circle getBounds()
{
return new Circle(p.x, p.y, radius);
}
小行星类:
public void update()
{
for (Asteroid a : asteroids)
{
a.update(Gdx.graphics.getDeltaTime(), xMin, xMax, yMin, yMax);
}
for (int i = 0; i < anzahl; 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())
{
berechneKollision(a1, a2);
}
}
}
}
public void render(ShapeRenderer renderer)
{
for (Asteroid a : asteroids)
{
renderer.setColor(1, 1, 0, 1);
renderer.circle(a.getP().x, a.getP().y, a.getRadius());
}
}
太空船类:
public Circle getBounds(){
return new Circle(sprite.getX(), sprite.getY(), sprite.getWidth());
}
CollisionManager:
public class CollisionManager extends Asteroids {
private Spaceship s = new Spaceship();
public void checkCollisions(){
for(int i = 0; i < asteroids.size(); i++)
{
if(asteroids.get(i).getBounds().contains(s.getBounds()))
{
//asteroids.remove(asteroids);
System.out.println("KOLLISION");
}
}
}
}
屏幕:
public abstract class Screen {
public abstract void create();
public abstract void render(SpriteBatch batch);
public abstract void update();
public abstract void update(ShapeRenderer renderer);
public abstract void update(OrthoCamera cam);
public abstract void resize(int width, int height);
public abstract void render(ShapeRenderer renderer);
public abstract void dispose(int width, int height);
public abstract void dispose();
public abstract void pause();
public abstract void resume();
public abstract void checkCollisions();
}
MenuScreen:
public class MenuScreen extends Screen {
private OrthoCamera cam;
private Spaceship spaceship;
private Asteroids asteroids;
CollisionManager cm;
.
.
.
public void checkCollisions()
{
cm.checkCollisions();
}
主类:
public class MyGdxGame implements ApplicationListener {
SpriteBatch batch;
ShapeRenderer renderer;
OrthoCamera cam;
public static int WIDTH = 1080 , HEIGHT = 720; // resolution
@Override
public void create() {
batch = new SpriteBatch();
renderer = new ShapeRenderer();
ScreenManager.setScreen(new MenuScreen());
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().checkCollisions();
ScreenManager.getCurrentScreen().update();
ScreenManager.getCurrentScreen().update(renderer);
ScreenManager.getCurrentScreen().update(cam);
}
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().render(batch);
ScreenManager.getCurrentScreen().render(renderer);
}
}
答案 0 :(得分:0)
您需要致电
shaperenderer.begin(ShapeRenderer.ShapeType type)
在绘制之前和
shaperenderer.end();
在astroids类的渲染结束后绘制。