2014-06-12 45 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