2017-01-11 58 views
0

所以我想創造一個遊戲,這是我第一次。我的遊戲是一款2D側面卷軸,關於避開流星的空間玩家。我已經成功設法讓流星在屏幕上隨機產生x和y軸,並在屏幕通過後重新定位。LibGDX - 如何在相距一定距離處產生物體?

但是我現在面臨的問題是有時流星的產卵會聚集在一起,我不想要。我如何讓流星在相距一定距離的地方產卵,這樣它們就不會聚集在一起。我找不到任何好的教程,或者任何人都可以指出我正確的方向。以下是我的代碼。

流星類

public class Meteors { 
    private Texture bigMeteor; 
    private Vector2 posBigMeteor; 
    private Random yrand; 

    //Constructor 
    public Meteors(float x){ 
     bigMeteor = new Texture("meteor.png"); 
     yrand = new Random(); 

     //Spawn location of meteor 
     posBigMeteor = new Vector2(x, yrand.nextInt(AstroDemo.HEIGHT/2 - bigMeteor.getHeight())); 
    } 

    public Texture getBigMeteor() { 
     return bigMeteor; 
    } 

    public Vector2 getPosBigMeteor() { 
     return posBigMeteor; 
    } 

    //Reposition the meteors 
    public void reposition(float x){ 
     posBigMeteor.set(x, yrand.nextInt(AstroDemo.HEIGHT/2 - bigMeteor.getHeight())); 
    } 
} 

PlayState類

public class PlayState extends State { 
    //Total meteor count on screen 
    private static final int METEOR_COUNT = 8; 

    private Naught naught; 
    private Texture bg; 
    private Random xrand; 
    private Array <Meteors> meteors; 

    public PlayState(GameStateManager gsm) { 
     super(gsm); 
     //Starting co-ordinates of main character (Naught) 
     naught = new Naught(50, 100); 

     //Setting viewport of the camera 
     cam.setToOrtho(false, AstroDemo.WIDTH/2, AstroDemo.HEIGHT/2); 
     bg = new Texture("bg.png"); 
     xrand = new Random(); 

     meteors = new Array <Meteors>(); 

     //Spawn meteors randomly off screen 
     for (int i = 1; i <= METEOR_COUNT; i++){ 
      meteors.add(new Meteors(AstroDemo.WIDTH/2 + (xrand.nextInt(300)))); 
     } 
    } 

    @Override 
    protected void handleInput() { 
     //If screen/mouse is held 
     if(Gdx.input.isTouched()){ 
      //Main Character jumps/flys 
      naught.jump(); 
     } 
    } 

    @Override 
    public void update(float dt) { 
     handleInput(); 
     naught.update(dt); 

     //If meteors are left side of the screen, re-position to the right side of the screen 
     for(Meteors meteor : meteors){ 
      if (cam.position.x - (cam.viewportWidth/2) > meteor.getPosBigMeteor().x + meteor.getBigMeteor().getWidth()){ 
       meteor.reposition(meteor.getPosBigMeteor().x + (AstroDemo.WIDTH/2 + 20 + (xrand.nextInt(300)))); 
      } 
     } 
     cam.position.x = naught.getPosition().x + 80; 

     cam.update(); 
    } 

    @Override 
    public void render(SpriteBatch sb) { 
     //Adjust the spritebatch for co-ordinate system in relation to camera 
     sb.setProjectionMatrix(cam.combined); 
     sb.begin(); 
     //Draw background where the camera is 
     sb.draw(bg, cam.position.x - (cam.viewportWidth/2), 0); 
     sb.draw(naught.getTexture(), naught.getPosition().x, naught.getPosition().y); 

     for (Meteors meteor : meteors) { 
     sb.draw(meteor.getBigMeteor(), meteor.getPosBigMeteor().x, meteor.getPosBigMeteor().y); 
     } 
     sb.end(); 
    } 

    @Override 
    public void dispose() { 

    } 
} 
+0

在計算距離或僅計算x軸時,是否要考慮x和y軸?這[post](http://stackoverflow.com/questions/929773/calculating-the-distance-between-two-points)解釋瞭如何計算,你可以使用這些信息來解決你的問題。 – Squiddie

+0

@Squiddie Yh我仍然想要考慮x軸和y軸,所以我希望它們隨機產卵但不能彼此重疊。 從閱讀帖子我認爲我明白該怎麼做,但帖子提到兩點,而不是兩個對象,我相信半徑參與,我正在努力與數學,並把這些數學成java/libgdx語言。 – user218130

+0

我相信你的兩個對象包含座標嗎?這些都是要點。我想你的對象有一個'x'和一個'width',以獲得對象中間的座標,只需執行'x + width/2',和y和height一樣。使用這些點。 – Squiddie

回答

0

創建預定義的值的陣列,用於您的y位置,然後從該陣列獲得隨機值。

除以高度成子部分然後獲得從該部分隨機值,使每個隨機值不能與其他值碰撞。