0

I am a newbie in libgdx. I am working on a game which has a player body and I have given controls but I want to create enemies at specific time in my game. I have tried creating it but only the player body is been created and but not enemy body.

This is my code

public class Player
    implements Screen, InputProcessor {
    private Body polybody;
    private Player player;
    private World world;
    private Body enemybody;
    private Sprite polysprite;
    public final float width, height;
    private Vector2 movement = new Vector2();
    private float speed = 580;


    public Player(World world, float x, float y, float width) {
        this.width = width; //IMP
        height = width * 2;
        BodyDef polygon = new BodyDef();
        polygon.type = BodyType.DynamicBody;
        polygon.position.set(x, y); //

        PolygonShape poly = new PolygonShape();
        poly.setAsBox(width / 2, height / 2); //

        //fixture defn

        FixtureDef polyfixture = new FixtureDef();
        polyfixture.shape = poly;
        polyfixture.friction = 0.8f;  //
        polyfixture.restitution = 0.1f; //
        polyfixture.density = 3; //

        //creating actual body
        polybody = world.createBody(polygon);
        polybody.createFixture(polyfixture);
        // polybody.applyAngularImpulse(52, true);
        polysprite = new Sprite(new Texture("img/car.jpg"));

        polysprite.setSize(0.5f, 1); //size of mario
        polysprite.setOrigin(polysprite.getWidth() / 2, polysprite.getHeight() / 2);
        polybody.setUserData(polysprite);

        poly.dispose();
    }


    public void update() {
        polybody.applyForceToCenter(movement, true);
    }

    public Body getBody() {
        return polybody;
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub
    }

    final float step = 0.1f;
    float timeElapsed = 0f;

    @Override
    public void render(float delta) {
        timeElapsed += delta;
        while (timeElapsed > step) {
            timeElapsed -= step;
            Enemy();
        }
    }

    public void Enemy() {
        BodyDef enemy = new BodyDef();
        enemy.type = BodyType.DynamicBody;
        enemy.position.set(6, 3);
        PolygonShape enemypoly = new PolygonShape();
        enemypoly.setAsBox(2, 2);

        FixtureDef enemyfixture = new FixtureDef();
        enemyfixture.shape = enemypoly;
        enemyfixture.friction = 0.75f;
        enemyfixture.restitution = 0.1f;
        enemyfixture.density = 5;

        enemybody = world.createBody(enemy);
        enemybody.createFixture(enemyfixture);
        enemypoly.dispose();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub
    }


    @Override
    public void dispose() {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        switch (keycode) {
            case Keys.W:
                movement.y = speed;
                break;
            case Keys.S:
                movement.y = -speed;
                break;
            case Keys.D:
                movement.x = speed;
                break;
            case Keys.A:
                movement.x = -speed;
        }
        return true;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        switch (keycode) {
            case Keys.W:
                movement.y = 0;
                break;
            case Keys.S:
                movement.y = 0;
                break;
            case Keys.D:
                movement.x = 0;
                break;
            case Keys.A:
                movement.x = 0;
        }
        return true;

    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        movement.x = speed;
        Gdx.app.log("Example", "touch done ");
        return true;
    }
}

This is the player class that gets called first and after this on render method the enemy method gets called. The player body is working fine but I couldn't see any enemy body. Please help .Thanks in advance !!

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Anusha
  • 939
  • 3
  • 13
  • 31

2 Answers2

1

Apparently, your Player class is responsible for:

  • Handling inputs for controlling the game
  • the construction of enemy objects
  • rendering the entire world (handling time) and displaying the world and all objects
  • creating Textures (that is supposed to be created only once, not every time you make a Sprite!)

Aaaand you don't have any other class.

I think you should fix this problem.

Also, I'm a bit weary of that you're calling dispose() each time after you create an object.

By the way, the render() method only gets called if you set the active screen for the Game, so

public class MyGame extends Game {
    ...
    MyScreen myScreen = new MyScreen();
    ...

    public void goToMyScreen() { 
        this.setScreen(myScreen);
    }
}

Something like that is necessary for render() to work.

And please check How to track time in Libgdx(android) on how to handle elapsed time.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 1
    If you are new, check out these 2 tutorials on the official libgdx wiki. [link](https://github.com/libgdx/libgdx/wiki/A-simple-game) [link](https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game) – Matt Mar 02 '15 at 09:31
0

I suggest a refactoring of your application. That class Player doesn't look like the right place for creating enemies (which you seem to do in every step). Instead you should create the Enemy in the same place where you create your Player and give your Enemy an own class.

Furthermore you could write debug messages to the console or use your IDE's debugger to check what your code is actually doing.

Sebastian
  • 5,721
  • 3
  • 43
  • 69