2015-03-02 53 views
0

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

這是被稱爲第一玩家類,這對渲染方法後,敵人的方法被調用。玩家身體正常,但我看不到任何敵人的身體。請幫助。提前感謝!

+0

爲什麼在render()期間每次調用Enemy()?看起來很奇怪,我。 – Sebastian 2015-03-02 07:37:34

+0

我剛試過。我不知道該怎麼做。你能指導我嗎? @Sebastian – Anusha 2015-03-02 08:16:43

回答

0

我建議你的應用程序的重構。該類Player看起來不是創造敵人的正確場所(你似乎在每一步都會這樣做)。相反,您應該在創建Player的相同位置創建Enemy,併爲您的Enemy分配一個自己的類。此外,您可以將調試消息寫入控制檯或使用您的IDE調試器來檢查您的代碼實際正在執行的操作。

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

如果您是新手,請查看官方libgdx wiki上的這兩個教程。 (https://github.com/libgdx/libgdx/wiki/A-simple-game) [link](https://github.com/libgdx/libgdx/wiki/Extending-the-simple-遊戲) – Matt 2015-03-02 09:31:51

相關問題