S
S
Sland Show2016-11-30 22:33:41
Java
Sland Show, 2016-11-30 22:33:41

Why doesn't it go to create Body through another class?

I welcome everyone.
The problem is that there is no way I can create a DynamicBody using another class.
I have a Player class:

public class Player extends Sprite {

    public World world;
    public Body b2body;

    public Player(World world) {
        this.world = world;
        definePlayer();
    }


    // create player
    public void definePlayer() {
        BodyDef bdef = new BodyDef();
        bdef.position.set(32 / MyGdxGame.PPM, 200 / MyGdxGame.PPM);
        bdef.type = BodyDef.BodyType.DynamicBody;
        b2body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(5 / MyGdxGame.PPM);

        fdef.shape = shape;
        b2body.createFixture(fdef);

    }

}

And in the main class I run it like this:
world = new World(new Vector2(0, -10), true);
player = new Player(world); // create player

Well, then everything is perfectly rendered by itself:
// clear game screen
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // render game map
        render.render();

        // render box2dbodies
        b2dr.render(world, gameCam.combined);

        // set batch to draw camera
        MyGdxGame.batch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();

So I'm trying to do the same trick with the Enemy class , but it didn't work. Nothing works at all.
And the most interesting thing is that if you write this separately in the main class:
BodyDef bdef = new BodyDef();
        bdef.position.set(32 + 10 / MyGdxGame.PPM, 200 + 10 / MyGdxGame.PPM);
        bdef.type = BodyDef.BodyType.DynamicBody;
        super.b2body = super.world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(20 / MyGdxGame.PPM);

        fdef.shape = shape;
        super.b2body.createFixture(fdef);

That will all work.
What am I doing wrong and why doesn't it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2016-11-30
@jamakasi666

Where is the physics update? Where is the .dispose() of a shape that is no longer needed after being passed to the fixture? Most importantly, where is the Exception? Telepaths on vacation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question