我想要得到子彈的起點,但我不能讓它最終,並且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();
}
}
}
}
也許創建一個'Bullet'類派生自'Circle'。然後,您可以爲原始位置創建屬性。 –
不知道如何做一個新班級 – user3735892