How to get the starting point of each bullet? Libgdx simple game

十年热恋 提交于 2019-12-13 07:57:27

问题


I want to get the starting point of the bullet but i can't make it final, and the bullet.x and bullet.y is always changing so i'm not sure how to save the starting co-ordinates of each bullet. It spawns in a random place and then moves. I've tried saving the bullet.x and bullet.y to a variable when it starts but that changes when the bullet moves as well

Here is my code:-

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:


My libgdx is a little rusty. That said, this looks like a fairly regular design problem. Essentially, your MyGame class is trying to do too much, which means solving problems within it is difficult.

Right now, you're trying to just use a circle as your bullet, which means you only get what the Circle class provides. You want to store extra information and add behavior to the circle, so you should create something to do that that. I'd suggest creating a Bullet class, something like this:

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...
}

You could alternatively have Bullet extend Circle, though that would make changing your bullet to something like a rectangle slightly more difficult if you ever decide to.

Then you can store additional information. It also lets you move some behaviour into that class, instead of doing everything inside your MyGame class. This is always good, as it makes code easier to understand.



来源:https://stackoverflow.com/questions/24195154/how-to-get-the-starting-point-of-each-bullet-libgdx-simple-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!