LibGdx如何创建多重子弹?

时间:2016-03-10 13:45:14

标签: java libgdx

我希望在触摸jetActor时显示并发射子弹,它可以是无限的子弹。我认为它可以通过某种方式完成数组,但是我尝试的越多,我理解的就越少。当演员被触摸时也是如此子弹,速度更快。

这是代码,它现在所做的就是显示喷气机和子弹,当喷射机被触及时它会发射#34;子弹。

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 () {

}

}

1 个答案:

答案 0 :(得分:1)

这会增加无限的子弹。它的问题是从0到无限需要永远。所以最好没有无限的子弹......

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

如果要绘制这些项目符号,只需循环列表并更新每个项目符号。

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

但由于这些子弹是演员,你可以将它们添加到舞台上,这样舞台就会为你处理act()draw()

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

但是你的代码有很多错误。在继续学习之前,你最好学习一些基本的Java。

bulletActor = new BulletActor[10];

不会为您创建10个项目符号。它会创建一个可能为您保留10个子弹的阵列。声明字段时,您可以将其视为一个与您为其创建的对象大小完全相同的框。

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();
}

这只是您应该了解的基础知识的一小部分。目前你咬的方式多,你可以咀嚼,所以做一个真正的好帮助,并学习所有关于面向对象编程的基础知识。