简体   繁体   中英

LibGdx How do i create multiply bullets?

I want to display and fire a bullet when jetActor is touched, it can be unlimited bullets.I think it can be done with arrays somehow, but the more I try , the less i understand.Also when the actor is touched multiply times,the bullet goes faster.

This is the code and all it does now is that it displays a jet and a bullet and when jet is touched it "fires" the bullet.

public class MyGdxGame implements ApplicationListener{

private Texture bulletTexture;
private Texture jetTexture;
private Stage stage;
private BulletActor[] bulletActor;
private JetActor jetActor;

float bulletX = 650, bulletY = 200;
float jetX = 700,jetY = 150;
boolean started;

public class JetActor extends Actor{

    public JetActor() {
        setBounds(jetX,jetY,jetTexture.getWidth(),jetTexture.getHeight());
        this.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
                started = true;
                bulletActor[2] = new BulletActor();
                System.out.println("Touched");
                return true;
            }
        });

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(jetTexture, jetX, jetY, jetTexture.getWidth(), jetTexture.getHeight());
    }
}

public class BulletActor extends Actor{
    @Override
    public void act(float delta) {
        if(started){
            bulletX -=3;
        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(bulletTexture,bulletX,bulletY,bulletTexture.getWidth(), bulletTexture.getHeight());
    }
}

@Override
public void create() {
    bulletTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\bullet.png");
    jetTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\jet.png");
    stage = new Stage();

    jetActor = new JetActor();
    bulletActor = new BulletActor[10];
    stage.addActor(jetActor);

    Gdx.input.setInputProcessor(stage);

    bulletActor[1] = new BulletActor();
    stage.addActor(bulletActor[1]);

}


@Override
public void dispose() {
}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

@Override
public void resize ( int width, int height){
}

@Override
public void pause () {
}

@Override
public void resume () {

}

}

This would add unlimited bullets. The problem with it is that going from 0 to unlimited takes forever. So better not have unlimited bullets...

List<BulletActor> bulletList = new ArrayList<BulletActor>();
for (int i = 0; i < unlimited; i++)
{
  bulletList.add(new bullet();
}

When you want to draw these bullets, just loop the list and update each individual bullet.

for (Bullet bullet : bulletList)
{
  bullet.act();
  bullet.draw();
}

But since these bullets are actors you might as well just add them to the stage so the stage will take care of act() and draw() for you.

//If you want to store them you still need a list or other datastructure to hold them.
List<BulletActor> bulletList = new ArrayList<BulletActor>();
for (int i = 0; i < unlimited; i++)
{
  bulletList.add(new bullet(); 
  stage.addActor(new BulletActor); // <-- Just adding them to the stage takes care of the act and draw methods.
}

But there is a lot more wrong with your code. You are really better off learning some basic Java before you continue.

bulletActor = new BulletActor[10];

Does not create 10 bullets for you. It creates a array that could potentially hold 10 bullets for you. When you declare a field you can see that as a box that is exactly the size of the object you create it for.

BulletActor myBullet // <-- Empty box (Null) where 1 BulletActor can be stored.

new BulletActor() // <-- A Bullet actor object

BulletActor myBullet = new BulletActor() // <-- Box with a BulletActor in it

BulletActor[] bulletArray = new BulletActor[10] // <-- 10 empty boxes

//Now store bullets in these boxes
for (int i = 0; i < bulletArray.length; i++)
{
    bulletArray[i] = new BulletActor();
}

This is only a fraction of the basics you should know. Currently you are biting way more then you can chew so do yourself a real good favor and learn all the basics about object oriented programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM