libgdx arrays onTouch() method and delays for objects

| | August 5, 2015

I am trying to create random bullets but it is not working for some reason. also how can I make a delay so the bullets come every 30 seconds or 1 minute?
Also the onTouch method does not work and it is not taking the bullet away?

Shall I put the array in the GameRender class?

bullet class

public class Bullet extends Sprite {

public static final float BULLET_HOMING = 6000; 
public static final float BULLET_SPEED = 300; 
private Vector2 velocity;
private float lifetime;
private Rectangle bul;

public Bullet(float x, float y) {
 velocity = new Vector2(0, 0);
     setPosition(x, y);
AssetLoader.bullet1.flip(true, false);
AssetLoader.bullet2.flip(true, false);
setSize(AssetLoader.bullet1.getWidth(), AssetLoader.bullet1.getHeight());
bul = new Rectangle();
}

public void update(float delta) {
 float targetX = GameWorld.getBall().getX();
 float targetY = GameWorld.getBall().getY();
 float dx = targetX - getX();
 float dy = targetY - getY();

 float distToTarget = (float) Math.sqrt(dx * dx + dy * dy); 
 dx /= distToTarget;
     dy /= distToTarget;
 dx *= BULLET_HOMING;
 dy *= BULLET_HOMING;
 velocity.x += dx * delta;
 velocity.y += dy * delta;

 float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
 velocity.x /= vMag;
 velocity.y /= vMag;
 velocity.x *= BULLET_SPEED;
 velocity.y *= BULLET_SPEED;

 bul.set(getX(), getY(), getOriginX(), getOriginY());

 Vector2 v = velocity.cpy().scl(delta);
 setPosition(getX() + v.x, getY() + v.y);
 setOriginCenter(); 
 setRotation(velocity.angle());
 }

public Rectangle getBounds() {
return bul;
}
public Rectangle getBounds1() {
return this.getBoundingRectangle();
}
}

game world class

public class GameWorld {

public static Ball ball;
private Bullet bullet1;
    private ScrollHandler scroller;
    private Array<Bullet> bullets = new Array<Bullet>();

    public GameWorld() {
    ball = new Ball(280, 273, 32, 32);
    bullet = new Bullet(-300, 200);
    scroller = new ScrollHandler(0);
    bullets.add(new Bullet(bullet.getX(), bullet.getY()));

    bullets = new Array<Bullet>();

    Bullet bullet = null;
    float bulletX = 0.0f;
    float bulletY = 0.0f;
    for (int i=0; i < 10; i++) {
       bulletX = MathUtils.random(-10, 10);
       bulletY = MathUtils.random(-10, 10);
       bullet = new Bullet(bulletX, bulletY);

       bullets.add(bullet); 
    }
}

public void update(float delta) {
    ball.update(delta);
    bullet.update(delta);
    scroller.update(delta);
}

public static Ball getBall() {
    return ball;
}

public ScrollHandler getScroller() {
    return scroller;
}

public Bullet getBullet1() { 
    return bullet1;
}
}

This is my game render class

public class GameRenderer {
private Bullet bullet;
 Ball ball;

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

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

 private void initGameObjects() {
 ball = GameWorld.getBall();
 bullet = myWorld.getBullet();
 scroller = myWorld.getScroller();
 }

 private void initAssets() {
 ballAnimation = AssetLoader.ballAnimation;
 bulletAnimation = AssetLoader.bulletAnimation;
 }

 public void render(float runTime) {

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

 batcher.begin();

 batcher.disableBlending();


 batcher.enableBlending();


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

    batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY(), bullet.getOriginX(), bullet.getOriginY(), bullet.getWidth(), bullet.getHeight(), 1.0f, 1.0f, bullet.getRotation());

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

I also tried this and it is not working, I used this in the GameRender class

Array<Bullet> enemies=new Array<Bullet>();

//in the constructor of the class
enemies.add(new Bullet(bullet.getX(), bullet.getY()));

 // this throws an exception for some reason??? this is in the render method
for(int i=0; i<bullet.size; i++)
   bullet.get(i).draw(batcher);

//this I am using in any method that will allow me from the constructor to update to render
for(int i=0; i<bullet.size; i++)
bullet.get(i).update(delta);

this is not taking the bullet out

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for(int i=0; i<bullet.size; i++)
 if(bullet.get(i).getBounds().contains(screenX,screenY))
     bullet.removeIndex(i--);
return false;
}

Thanks for the help anyone.

One Response to “libgdx arrays onTouch() method and delays for objects”

  1. Please try the following, went through it again and I think this is the issue:

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
       for(int i=0; i<bullet.size; i++)
       {
          if(bullet.get(i).getBounds().contains(screenX,screenY))
             bullet.removeIndex(i);
             //with bullet.removeIndex(i--) - you are removing the previous index 
             //from the array indifferent if it's bounding box touched the screen position from onTouch.
       }
       return false;
    }
    

Leave a Reply