0
\$\begingroup\$

I created a Jet and a Bullet, and when Jet is touched, the Bullet is fired.

Now I want to know how can I make the Jet fire again when touched after it has fired the first shot?

public class MyGdxGame implements ApplicationListener{

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

    int counter=0;
    float jetX = 700,jetY = 150;
    float bulletX = jetX-10, bulletY = jetY +43;
    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){
                    stage.addActor(bulletActor);
                    System.out.println(counter);
                    counter++;
                    started = true;
                    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) {
        }

        @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();
        stage.addActor(jetActor);
        Gdx.input.setInputProcessor(stage);
    }


    @Override
    public void dispose() {
    }

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

        if(started){
            bulletX -=5;
        }
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

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

    @Override
    public void pause () {
    }

    @Override
    public void resume () {

    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Right now you declare a BulletActor as a member of this game class, and whenever you get a touch input you call addActor, giving it that bullet. All this does is add the same bullet over and over. I'm not super familiar with the details of the APIs you are using, but I suspect that's basically a no-op.

What you want to do instead is create a new bullet every time you get a touch event:

            public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
                BulletActor newBullet = new BulletActor;

                // Configure the new bullet; you may need to assign position
                // and velocity here, or in the bullet constructor.
                // You also may need to assign other properties appropriate
                // to your game.
                newBullet.x = x;
                newBullet.y = y;
                // ...etc...

                stage.addActor(newBullet);
                System.out.println(counter);
                counter++;
                started = true;
                return true;
            }

Create the new bullet, set it's initial properties so it's located where you touched or where the JetActor is or whatever else you need, and add that actor to the scene. You probably won't need to the bulletActor member of the game class any more.

However if you do want to be able to modify properties of the bullets after creation, or if you need to retain references to them in order to be able to correct deallocate them later, you may want to create a dynamic array (ArrayList) of bullets and store each newly-created bullet in there for later reference.

\$\endgroup\$
2
  • \$\begingroup\$ But how do i set the starting point and then add the velocity to the bullet?Do i need to give int x, int y to the Bullet() constructor ?And then how do i add the velocity? \$\endgroup\$ Mar 13, 2016 at 13:36
  • \$\begingroup\$ @WannabeHacker You do need to set the new bullet's X and Y (and velocity) when you create the bullet, yes. I can't tell you exactly how because it depends on the API of BulletActor, which you don't provide. As for updating the position of the bullets every frame, you'd have to loop through every bullet and add its velocity to its position every frame, just like you do with the current bullet. You just need to scale it up to an array so you can handle many bullets. \$\endgroup\$
    – user1430
    Mar 13, 2016 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .