简体   繁体   中英

Array of Bodies in Box2D (Java)

I'm pretty new to Box2D and have very little programming experiences at all, so please be patient. Currently I am working on a little Breakout game.Something like a really simple version of this: http://i.computer-bild.de/imgs/4/9/8/6/7/5/1/Google-Breakout-745x419-9c0b3d2ebbdeae82.jpg

It is an exercise for my university. At this point I already created the paddle, the ball and the walls. Now I want to create the bricks. My problem is that I'm not sure how to organize them. I thought about making a class for the bricks with 2 floats in the constructer for the actual position of the one brick. Then I wanted to create Arrays of the brick class.

At this point my code looks like this:

private Body brickBody;
private PolygonShape brickShape;
private BodyDef brickBodyDef;
private Fixture brickFixture;
Physik phy;


public CleverBrick(float a, float b, final Physik p) {


    brickBodyDef = new BodyDef();
    brickBodyDef.type = BodyType.StaticBody;
    brickBodyDef.position.set(new Vector2(a,b));
    phy =p;

    brickBody = Physik.getWorld().createBody(brickBodyDef);

    brickShape = new PolygonShape();
    brickShape.setAsBox(30,5);


    Fixture brickFixture = brickBody.createFixture(brickShape, 0.0f);
    brickFixture.setUserData("The brick");

}


public void destroyBrick() {
    brickBody.destroyFixture(brickFixture);
}


public Body getBrickbody() {
    return brickBody;
}


public void setBrickbody(Body brickbody) {
    this.brickBody = brickbody;
}
public PolygonShape getBrickShape() {
    return brickShape;
}
public void setBrickShape(PolygonShape brickShape) {
    this.brickShape = brickShape;
}


public BodyDef getBrickBodyDef() {
    return brickBodyDef;
}


public void setBrickBodyDef(BodyDef brickBodyDef) {
    this.brickBodyDef = brickBodyDef;
}


public Fixture getBrickFixture() {
    return brickFixture;
}


public void setBrickFixture(Fixture brickFixture) {
    this.brickFixture = brickFixture;
}

}

And I try to create the array with this in the main class with these lines:

    for (int i =0; i<9; i++) {
        bricks[i] = new CleverBrick(100,100, this);
    } 

Later I want to import different structures of bricks from xml files, this is just a test case.

I always get a NullPointerException at the line : brickBody = Physik.getWorld().createBody(brickBodyDef); and I dont know why. I think the problem is about getting the world from

I hope someone can help me with that.

Couple of questions:

  • what is the class Physik.java?
  • why do you use floats for the Brick positions? these don't seem like floating point numbers to me, an int feels natural here
  • regarding NPE - @Atuos is right - watch what is null. Here it seems that either Physik or getWorld() are null values. Shouldn't you write

     physik.getWorld.createBody() 

Either way - it would be nice if you could amend your question by clearly stating what is it that you're asking and providing more source code - the whole class CleverBrick and Physik

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM