Home Game Development Multiple enemy array in LibGDX

Multiple enemy array in LibGDX

0
Multiple enemy array in LibGDX

[ad_1]

I’m attempting to make a a number of enemy array, the place each 30 secods a brand new bullet comes from a random level. And if the bullet is clicked it ought to disapear and a pop like an explosion ought to seem. And if the bullet hits the ball then the ball pops.
so the bullet ought to change to a unique sprite or texture. identical with the ball pop.

But all that occurs is the bullet if touched pops and nothing else occurs. And if modified then the bullet retains flashing because the replace is means an excessive amount of.

I’ve added COMMENTS within the code to elucidate extra on the problems.

beneath is the code.
if extra code is required i’ll present.

Thank you

public class GameRenderer {

non-public GameWorld myWorld;
non-public OrthographicCamera cam;
non-public ShapeRenderer shapeRenderer;
non-public SpriteBatch batcher;

// Game Objects
non-public Ball ball;
non-public ScrollHandler scroller;
non-public Background background;
non-public Bullet bullet1;
non-public BulletPop bPop;

non-public Array<Bullet> bullets;

    // This is for the delay of the bullet coming one after the other each 30 seconds.
/** The time of the final shot fired, we set it to the present time in nano when the article 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
non-public TextureRegion bg, bPop;
non-public Animation bulletAnimation, ballAnimation;
non-public Animation ballPopAnimation;

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

    batcher = new SpriteBatch();
    // Attach batcher to digicam
    batcher.setProjectionMatrix(cam.mixed);

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

            // This is suppose to supply 10 bullets at random locations 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 strategies to initialize occasion variables
    initGameObjects();
    initAssets();
}

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

non-public 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.measurement; i++) {
        if (bullets.get(i).getBounds().comprises(0, 0))
            bullets.removeIndex(i);
    }
}

non-public 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.start();
    // Disable transparency 
    // This is sweet for efficiency when drawing photographs that don't require
    // transparency.
    batcher.disableBlending();

    drawBackground();

    batcher.enableBlending();

            // when the bullet hits the ball, it must be disposed or taken away and a ball pop sprite/texture must be put as a replacement
    if (bullet1.collides(ball)) {
                    // attracts the bPop texture however the bullet doesn't go simply retains going round, 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());

           // that is the place i'm attempting to make the bullets come one after the other, and if eliminated through the onClick() then bPop animation 
           // ought to play however doesn't???
    if(TimeUtils.nanoTime() - lastShot > shotFreq){
         // Create your stuff
        for (int i = 0; i < bullets.measurement; 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 vital to set the final shot to now, or it's going to mess up and go full auto */
         lastShot = TimeUtils.nanoTime();
      }

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

Thank you

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here