johnny-b
johnny-b

Reputation: 88

Multiple enemy array in LibGDX

I am trying to make a multiple enemy array, where every 30 secods a new bullet comes from a random point. And if the bullet is clicked it should disapear and a pop like an explosion should appear. And if the bullet hits the ball then the ball pops. so the bullet should change to a different sprite or texture. same with the ball pop.

But all that happens is the bullet if touched pops and nothing else happens. And if modified then the bullet keeps flashing as the update is way too much.

I have added COMMENTS in the code to explain more on the issues.

below is the code. if more code is needed i will provide.

Thank you

public class GameRenderer {

private GameWorld myWorld;
private OrthographicCamera cam;
private ShapeRenderer shapeRenderer;
private SpriteBatch batcher;

// Game Objects
private Ball ball;
private ScrollHandler scroller;
private Background background;
private Bullet bullet1;
private BulletPop bPop;

private Array<Bullet> bullets;

    // This is for the delay of the bullet coming one by one every 30 seconds.
/** The time of the last shot fired, we set it to the current time in nano when the object is first created */
double lastShot = TimeUtils.nanoTime();
  /** Convert 30 seconds into nano seconds, so 30,000 milli = 30 seconds */
 double shotFreq = TimeUtils.millisToNanos(30000);

// Game Assets
private TextureRegion bg, bPop;
private Animation bulletAnimation, ballAnimation;
private Animation ballPopAnimation;

public GameRenderer(GameWorld world) {
    myWorld = world;
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 480, 320);

    batcher = new SpriteBatch();
    // Attach batcher to camera
    batcher.setProjectionMatrix(cam.combined);

    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(cam.combined);

            // This is suppose to produce 10 bullets at random places on the background.
    bullets = new Array<Bullet>();
    Bullet bullet = null;
    float bulletX = 00.0f;
    float bulletY = 00.0f;
    for (int i = 0; i < 10; i++) {
        bulletX = MathUtils.random(-10, 10);
        bulletY = MathUtils.random(-10, 10);
        bullet = new Bullet(bulletX, bulletY);

        AssetLoader.bullet1.flip(true, false);
        AssetLoader.bullet2.flip(true, false);

        bullets.add(bullet);
    }

    // Call helper methods to initialize instance variables
    initGameObjects();
    initAssets();
}

private void initGameObjects() {
    ball = GameWorld.getBall();
    bullet1 = myWorld.getBullet1();
    bPop = myWorld.getBulletPop();
    scroller = myWorld.getScroller();
}

private void initAssets() {
    bg = AssetLoader.bg;
    ballAnimation = AssetLoader.ballAnimation;
    bullet1Animation = AssetLoader.bullet1Animation;
    ballPopAnimation = AssetLoader.ballPopAnimation;
}

    // This is to take the bullet away when clicked or touched.
public void onClick() {
    for (int i = 0; i < bullets.size; i++) {
        if (bullets.get(i).getBounds().contains(0, 0))
            bullets.removeIndex(i);
    }
}

private void drawBackground() {
    batcher.draw(bg1, background.getX(), background.getY(), background.getWidth(), backgroundMove.getHeight());
}

public void render(float runTime) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    batcher.begin();
    // Disable transparency 
    // This is good for performance when drawing images that do not require
    // transparency.
    batcher.disableBlending();

    drawBackground();

    batcher.enableBlending();

            // when the bullet hits the ball, it should be disposed or taken away and a ball pop sprite/texture should be put in its place
    if (bullet1.collides(ball)) {
                    // draws the bPop texture but the bullet does not go just keeps going around, and the bPop texture goes.
        batcher.draw(AssetLoader.bPop, 195, 273);
    }

    batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

           // this is where i am trying to make the bullets come one by one, and if removed via the onClick() then bPop animation 
           // should play but does not???
    if(TimeUtils.nanoTime() - lastShot > shotFreq){
         // Create your stuff
        for (int i = 0; i < bullets.size; i++) {
            bullets.get(i);
            batcher.draw(AssetLoader.bullet1Animation.getKeyFrame(runTime), bullet1.getX(), bullet1.getY(), bullet1.getOriginX(), bullet1.getOriginY(), bullet1.getWidth(), bullet1.getHeight(), 1.0f, 1.0f, bullet1.getRotation());
            if (bullets.removeValue(bullet1, false)) {
                batcher.draw(AssetLoader.ballPopAnimation.getKeyFrame(runTime), bPop1.getX(), bPop1.getY(), bPop1.getWidth(), bPop1.getHeight());
            }
        }
         /* Very important to set the last shot to now, or it will mess up and go full auto */
         lastShot = TimeUtils.nanoTime();
      }

    // End SpriteBatch
    batcher.end();
}
}

Thank you

Upvotes: 1

Views: 1502

Answers (2)

Will Kru
Will Kru

Reputation: 5212

In addition to MilanG's answer

The bullets.get(i); line does nothing.. You'll want to store the returned Bullet into a variable, for which it seems you created the bullet1 var.

Also, you really shouldn't add elements to or remove elements from an array while looping through it. Consider using a second array for elements to be added/removed and use that to alter the main array or use an iterator.

[edit] In this particular case you could also do something like this, though it would only work for one bullet per click

int index = -1;
for (int i = 0; i < bullets.size; i++) {
    if (bullets.get(i).getBounds().contains(0, 0)) {
        index = i;
        break;
    }
}
if(index > -1) bullets.removeIndex(index);

It also seems your .contains() should be passed the clicked position instead of 0,0?

Upvotes: 1

MilanG
MilanG

Reputation: 7124

Hmm...why are you drawing graphics from inside of the if where you are adding new bullets? This way all you draw will be drown only one frame per 30 seconds. Inside that if you should only add/remove objects and draw them outside, all the time. No drawing inside that if!

Upvotes: 1

Related Questions