Multiple enemy array in LibGDX

Posted by johnny-b on Game Development See other posts from Game Development or by johnny-b
Published on 2014-06-12T22:02:54Z Indexed on 2014/06/13 3:42 UTC
Read the original article Hit count: 240

Filed under:
|
|

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

© Game Development or respective owner

Related posts about java

Related posts about android