如何在libgdx中的特定时间创建敌人

时间:2015-03-02 06:48:08

标签: java libgdx

我是libgdx的新手。我正在制作一个拥有玩家身体的游戏,我已经给出了控制但我想在游戏中的特定时间制造敌人。我尝试过创造它,但只创建了玩家身体而不是敌人身体。

这是我的代码

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

这是首先被调用的玩家类,然后在render方法中调用敌人方法。玩家身体工作正常但我无法看到任何敌人的身体。请帮忙。谢谢!

2 个答案:

答案 0 :(得分:1)

显然,您的Player班负责:

  • 处理控制游戏的输入
  • 建造敌人物品
  • 渲染整个世界(处理时间)并显示世界和所有对象
  • 创建纹理(应该只创建一次,而不是每次都创建Sprite!)

Aaaand你没有任何其他课程。

我认为你应该解决这个问题。

另外,在您创建对象后,每次调用dispose()时我都会感到有点厌倦。

顺便说一句,只有在为游戏设置活动屏幕时才会调用render()方法,所以

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

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

这样的事情是render()工作所必需的。

请检查How to track time in Libgdx(android)如何处理已用时间。

答案 1 :(得分:0)

我建议重构您的应用程序。那个班级Player看起来不是创造敌人的好地方(你似乎在每一步都做过)。相反,您应该在创建Enemy的同一位置创建Player,并为您的Enemy提供自己的课程。

此外,您可以将调试消息写入控制台,或使用IDE的调试器来检查代码实际执行的操作。