Hey first time posting about code so please say if you need more information than what i give you...

I'm having trouble with a timer, am trying to wait 2 secs then add 5 to Defender.Bullets but they are a delay on this and it bugs out if i keep clicking this. It works properly if i wait a couple of seconds after its been implemented.

Here is the code

new java.util.Timer().schedule( 
        new java.util.TimerTask() {
        @Override
        public void run() {

             Defender.bullets = 5;

        }
    }, 2000 (I have tried to put a delay here but didn't seem to work either)
);

The code has supposed to reload bullets which is up the right hand side of the screen but looks like the "Defender.bullets = 5;" keep going even after the 2 secs...

This is my first time using Timer, so not sure if this is done correctly.

Any information is appreciated
Thanks for your time

Edited 3 Years Ago by Dani: Formatting fixed

Defender.bullets is an array, this is being updated so i can fire more objects after 5 secs...

The GUI is constantly being updated under this...
Should i still use the java.swing over this, ive tried different ways with this and i couldn't find a way to do it for what am wanting.

Here is the full set of code for this part:

if(Defender.health > 0 && Defender.bullets > 0){
			
				for (Defender_Bullet defend: Dbullet){
					defend.draw(g);
					defend.update(this, 0);
				}
			
		}
		else if(Defender.health <= 0){
			
			Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 70);
			
			g.setFont(font);
			g.setColor(Color.RED);
			g.drawString("You Have Lost!", 85, 300);
			
		}
		else if(Defender.bullets == 0) {
			
			for (Defender_Bullet defend: Dbullet){
				defend.draw(g);
				defend.update(this, 0);
			}
			Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 12);
			
			g.setFont(font);
			g.setColor(Color.RED);
			g.drawString("Reloading", 35, 50);
		
			
				new java.util.Timer().schedule(
				        new java.util.TimerTask() {
				            @Override
				            public void run() {

				            	Defender.bullets = 5;
				         
				            }
				        }, 3000
				); 
		}
		
		Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 20);
		
		g.setFont(font);
		g.setColor(Color.YELLOW);
		g.drawString(Defender.bullets + "" , 15, 50);
		
		for (Enemy_Bullet Attacker: EBullet){
			Attacker.draw(g);
			Attacker.update(this, 0);	
		}

		Defender.draw(g);
		Defender.update(this, 1);
This article has been dead for over six months. Start a new discussion instead.