2
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Is all your sprites packed as a single sprite Sheet or they are packed separate?If they are packed as Single sprite sheet try to separate them...I had this flashing issue when I was trying to scroll background..Infinitely \$\endgroup\$
    – Shersha Fn
    Jan 6, 2016 at 19:02

1 Answer 1

1
\$\begingroup\$

I don't know how the Bullet class works, so I am just doing this from your code, but in your if statement:

    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);
}

You are not actually removing the bullet which is why the bullet keeps flying.

Also, in the loop for the bullets:

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());
        }
    }

You aren't storing bullets.get(i) anywhere. You should be writing:

for (int i = 0; i < bullets.size; i++) {
        Bullet b = bullets.get(i);//replace bullet1 before the if statement with b
        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());
        }
    }

And this if statement:

if (bullets.removeValue(bullet1, false))

Should be moved outside the loop as you only need to call it once. When the bullet is destroyed (i.e. in the collides if statement).

Your onClick() method should probably not be removing the bullet, if you want to do the explosion animation like you are doing. It could set that bullet to hidden, then in your render method you find all the hidden bullets and continue their explosion animation (although you might want to store which frame they're on somewhere.

They're might be some stuff that I missed, but I hope this helps a bit.

\$\endgroup\$
8
  • \$\begingroup\$ if you wish i can send the full code for you to look at???? \$\endgroup\$
    – johnny-b
    Jun 13, 2014 at 0:30
  • \$\begingroup\$ @johnny-b Do you understand what I tried to explain to you and have you tried it? \$\endgroup\$ Jun 13, 2014 at 2:26
  • \$\begingroup\$ Yes i did what you edited and asked me to do. hence i asked if you want to see the full code maybe you will understand more about it as it is bugging me now and my condition. forget that. just to figure out how to make this multiple random array with a time delay. Thanks \$\endgroup\$
    – johnny-b
    Jun 13, 2014 at 2:54
  • \$\begingroup\$ anyone any help here? all i am trying to do is have a bullet come and hit the target every 30 to 60 seconds... Can anyone help? hence the array method. if there any other way to do it i am okay with that. thank you. \$\endgroup\$
    – johnny-b
    Jun 14, 2014 at 0:45
  • \$\begingroup\$ i got the array sorted just the timing is not working. if(TimeUtils.nanoTime() - lastShot > shotFreq){ // Create your stuff lastShot = TimeUtils.nanoTime(); } \$\endgroup\$
    – johnny-b
    Jun 14, 2014 at 10:06

You must log in to answer this question.

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