-1

I'm currently working on a Project where the user has to avoid Asteroids (drawn with ShapeRenderer) with a Spaceship(Sprite). The Asteroids are saved in a List Array. Now i'm trying to create a collision method when Sapceship and Asteroids collide. To make it as simple as possible i want to draw a Circle around the Spaceship and check for Collision. Eclipse says i have a I have a java.lang.IllegalStateException: begin must be called first.

Asteroid Class:

public Circle getBounds()
{
   return new Circle(p.x, p.y, radius);
}

Asteroids Class:

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());
    }
}

Spaceship Class:

 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");
            }
        } 
    }

}

Screen:

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();
    }

Main Class:

 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);
    }
}
Enamul Hassan
  • 4,744
  • 22
  • 35
  • 52
Zui0per
  • 69
  • 10
  • All i see is a big chunk of code, but i fail to see your question... – SomeJavaGuy Aug 18 '16 at 12:39
  • 2
    [here's the duplicate SO question to your exception](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SomeJavaGuy Aug 18 '16 at 12:41
  • 1
    Have you actually assigned a value to `cm`? – Andy Turner Aug 18 '16 at 12:42
  • forgotten in the rush of code insertion – Zui0per Aug 18 '16 at 12:42
  • forgot to create an instance of cm, bu now i have a IllegalStateException at ShapeRenderer – Zui0per Aug 18 '16 at 12:50
  • "Eclipse says i have a java.lang.IllegalStateException: begin must be called first". The error message tells you exactly where to look, and the documentation is your friend. https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html . The shapes are rendered in batches, so whenever you use the shaperender you have to call begin (just like the error message says) and end. Look at the example code in the API doc. – Peter R Aug 18 '16 at 13:21
  • Welcome to Stack Overflow! Can you please have a better title and provide a brief in the content with your effort to solve the problem instead of having bunch of codes? – Enamul Hassan Aug 21 '16 at 17:18
  • @manetsus Ok, i will try next time. – Zui0per Aug 21 '16 at 19:03

1 Answers1

0

You need to call

shaperenderer.begin(ShapeRenderer.ShapeType type)

before drawing and

shaperenderer.end();

after drawing end in render of astroids class.

Deniz Yılmaz
  • 995
  • 8
  • 16