2014-06-12 44 views
0

我想要得到子彈的起點,但我不能讓它最終,並且bullet.x和bullet.y總是在變化,所以我不知道如何保存起始座標每顆子彈。它隨機產生,然後移動。我試着拯救bullet.x和bullet.y給一個變量在啓動時,但改變的時候子彈移動以及如何獲得每顆子彈的起點? Libgdx簡單遊戲

這裏是我的代碼: -

public class MyGame extends ApplicationAdapter { 
SpriteBatch batch; 
Texture ballImage, bulletImage; 
OrthographicCamera cam; 
Circle ball, bullet; 
Array <Circle> bullets; 
//long lastShot; 

@Override 
public void create() 
{ 
    System.out.println("game created"); 
    ballImage = new Texture(Gdx.files.internal("ball.png")); 
    bulletImage = new Texture(Gdx.files.internal("bullet.png")); 

    cam = new OrthographicCamera(); 
    cam.setToOrtho(true,320,480);//true starts top left false starts bottom left 

    batch = new SpriteBatch(); 

    ball = new Circle(); 
    ball.radius=20; 
    ball.x=320/2-ball.radius; // half screen size - half image 
    ball.y=480/2-ball.radius; 

    bullets = new Array<Circle>(); 
    spawnBullet(); 

} 

public void spawnBullet() 
{ 
    Circle bullet = new Circle(); 
    bullet.radius=8; 
    bullet.x=bullet.radius; // half screen size - half image 
    bullet.y=MathUtils.random(0, 480-bullet.radius); 
    bullets.add(bullet); 
    System.out.println("x: "+bullet.x+" Y: "+bullet.y); 

} 

@Override 
public void render() 
{ 
    Gdx.gl.glClearColor(0, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    cam.update(); 
    batch.setProjectionMatrix(cam.combined); 
    batch.begin(); 
    batch.draw(ballImage,ball.x-ball.radius,ball.y-ball.radius); 
    for(Circle bullet: bullets) 
    { 
     batch.draw(bulletImage, bullet.x-bullet.radius, bullet.y-bullet.radius); 
    } 
    batch.end(); 

    if(Gdx.input.isTouched()) 
    {   
     Vector3 pos = new Vector3(); 
     pos.set(Gdx.input.getX(), Gdx.input.getY(),0); 
     cam.unproject(pos); 
     ball.y = pos.y ;  
    } 


    if(ball.y<0+ball.radius)ball.y=ball.radius; 
    if(ball.y>480-ball.radius)ball.y=480-ball.radius; 

    Iterator<Circle> i = bullets.iterator();  

    while(i.hasNext()) 
    { 

     Circle bullet = i.next(); 
     //System.out.println("x2: "+bullet.x+" Y2: "+bullet.y); 


     if(bullet.y>240){ 
     bullet.x++; 
     bullet.y--;} 

     bullet.x++; 

     //right border collision 
     if(bullet.x>320) 
     { 
      i.remove(); 
      spawnBullet(); 
     } 
     //circle collision 
      if(ball.overlaps(bullet)) 
      { 
      i.remove(); 
      spawnBullet(); 
      } 
    } 
} 
    } 
+1

也許創建一個'Bullet'類派生自'Circle'。然後,您可以爲原始位置創建屬性。 –

+0

不知道如何做一個新班級 – user3735892

回答

3

我libgdx是一點點生鏽。這就是說,這看起來像一個相當常見的設計問題。從本質上講,你的MyGame類正在嘗試做太多,這意味着解決其中的問題是困難的。

現在,你只是想用一個圓作爲你的子彈,這意味着你只能得到Circle類提供的東西。你想存儲額外的信息並向圈子添加行爲,所以你應該創造一些事情來做到這一點。我建議創建一個子彈類,像這樣:

public class Bullet { 
    private Circle circle; 
    private int startX; 
    private int startY; 

    public Bullet(int startX, int startY){ 
     circle = //create circle from startX and startY 
     this.startX = startX; 
     this.startY = startY; 
    } 
    //getters and setters here... 
} 

您可以選擇性地具有子彈延長圈,儘管這將使改變你的子彈像一個矩形稍有難度,如果你決定。

然後您可以存儲更多信息。它還可以讓你將某些行爲轉移到該類中,而不是在MyGame類中做所有事情。這總是很好,因爲它使代碼更易於理解。

+0

我試過這樣做,但我不知道如何在mygame類中實現它 – user3735892