solved libgdx onTouch() (delays for objects, array) -to a point-

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?
thanks


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

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.
johny

Offering cash is not going to help you solve your issues. Learn Java before making games, that’s all I can say.

It will help solve my problems though…

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

Please post the error, I bet null pointer.

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

This is not working for the simple reason you are using i–, this means you are taking the value of i and decreasing it by 1. Just use i, you want to remove the bullet at index i.

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

What’s that all about? Why are you updating just 1 bullet when you in-fact have an array of them?

public void update(float delta) {
        ball.update(delta);
   for(Bullet bullet : bullets){
           bullet.update(delta);
   }
        scroller.update(delta);
    }

The correct way ^

For a delay you want to use a basic timer, so you want to make the bullets appear every xxx amount of time passed, lets say 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);

	public void update(float delta) {
		
		/** Now we take the current nano time, subtract the lastShot and check if that value is more than the frequency we shoot*/
		if(TimeUtils.nanoTime() - lastShot > shotFreq){
			// Create your stuff
			/* Very important to set the last shot to now, or it will fuck up and go full auto */
			lastShot = TimeUtils.nanoTime();
		}

The comments should be self expanatory, I presume you know basic Java, opiop has a point that offering cash does not solve anything, if anything it makes you look like a tool. If you are not comfortable with the language, take a step back and read some more books.

However don’t take his comments too harshly, he will be ok after a few coffees.

hey Gibbo thanks for the help.
i guess i am going to learn more as i tried and it is not working.
i feel like quitting programming as it was fun in the beggining now i find it too daunting as nothing is working.
thanks for the info.
johnny

You won’t get anywhere with that shite, not everyone is amazing at something right away, no one is really.

Keep going, there just comes a point where you just “click” and suddenly everything becomes clear, literally everything.

thanks for the incouragement.
i will keep trying i guess i cannot create arrays ??? i done what i can.
thanks

Tutorials Point Good for reference <<<

hey Gibbo thanks for the encouragement mate. ;D

figured it out with your help and some others.

main issue was the silly gameworld class and gamerenderer class >:(

so i merged them into the gamescreen class and all works fine now.

the way was shown by some amatuer and that is for someone who is really good at programming. someone who knows how to render in one class and update in another and more. newbie like me just stick to one.

thanks pal.