1

I'm trying to create new enemies from their own class. I try to pass World from another class as parameter however when trying to add new body to world it results a nullpointer. Here's the constructor from Enemy-class

public Enemy(float x, float y, World world){
    enemyTexture = new Texture(Gdx.files.internal("ball.bmp"));
    BodyDef myBodyDef = new BodyDef();
    myBodyDef.type = BodyDef.BodyType.DynamicBody;
    myBodyDef.position.set(x, y);

    FixtureDef enemyFixtureDef = new FixtureDef();
    enemyFixtureDef.density     = 1.5f;
    enemyFixtureDef.restitution = 0.1f;
    enemyFixtureDef.friction    = 0.5f;

    CircleShape circleshape = new CircleShape();
    circleshape.setRadius(enemyRadius);

    enemyFixtureDef.shape = circleshape;
    enemyBody = world.createBody(myBodyDef);
    enemyBody.setUserData("enemy");
    enemyBody.createFixture(enemyFixtureDef);
}
radoh
  • 4,554
  • 5
  • 30
  • 45
M1k1tus
  • 31
  • 6
  • On which line is the null pointer exception thrown? – Marco Altieri Feb 20 '16 at 15:14
  • When trying to access world. In this case the line with enemyBody = world.createBody(myBodyDef); – M1k1tus Feb 20 '16 at 15:17
  • could you show the code where the Enemy is cretaed to be sure that world object is not null ? – vincent Feb 20 '16 at 15:18
  • did you declare `enemyBody` at some point? something like `EnemyBodyClass enemyBody = new EnemyBodyClass(parameters)`? – PixelMaster Feb 20 '16 at 15:19
  • 1
    enemyBody was declared earlier in that class. However in my main class I tried to add enemy to world before it was created! Feel rather stupid having fought with this over a couple of days. Thanks guys! – M1k1tus Feb 20 '16 at 15:21

0 Answers0