繁体   English   中英

LibGdx如何创建多个项目符号?

[英]LibGdx How do i create multiply bullets?

我想在触摸jetActor时显示并发射子弹,它可以是无限的子弹。我认为可以以某种方式使用数组来完成,但是我尝试的次数越多,我的理解就越少。子弹走得更快。

这就是代码,现在所要做的就是显示喷气机和子弹,当触摸喷气机时会“发射”子弹。

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

}

}

这将增加无限的子弹。 它的问题在于,从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();
}

这只是您应该了解的基础知识的一小部分。 当前,您所咬的是更多的东西,然后您可以咀嚼,所以请给自己一个真正的好处,并学习有关面向对象编程的所有基础知识。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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