This site requires JavaScript, please enable it in your browser!
Greenfoot back
footpickle
footpickle wrote ...

2020/3/3

How do i use an if statement to check if a method has been called?

footpickle footpickle

2020/3/3

#
I'm trying to make a timer run so I can't constantly shoot, but then if I don't shoot for a long time, I still have to wait for the timer as it resets when it's done, not when I shoot. How would I make an if statement to check if my "fire" method has been called?
danpost danpost

2020/3/4

#
Do you mean this:
if (timer != 0)
footpickle footpickle

2020/3/5

#
Well, My bullet fires repeatedly, but with a short recharge. Is there a way to make the recharge only reset when the method is called?
danpost danpost

2020/3/5

#
footpickle wrote...
Well, My bullet fires repeatedly, but with a short recharge. Is there a way to make the recharge only reset when the method is called?
"method"? What method? Where called from?
footpickle footpickle

2020/3/5

#
I mean a thing called "fire()" happens
danpost danpost

2020/3/5

#
footpickle wrote...
I mean a thing called "fire()" happens
Only reset it in the fire method.
footpickle footpickle

2020/3/5

#
Ok, I'll try that :)
footpickle footpickle

2020/3/5

#
It didn't work. This is the code:
private void fire()
     {
         Bomb bomb = new Bomb();
         
         if (Greenfoot.isKeyDown("space"))
         getWorld().addObject(bomb, getX(), getY());
         bomb.setRotation(getRotation());
         timer = 15;
     }
danpost danpost

2020/3/5

#
You are missing brackets for your if block.
danpost danpost

2020/3/5

#
Also, you are creating a bomb every time the method is called -- unnecessarily. Create the bomb inside the if block. Where are you regulating creation of bombs based on the timer (if (timer == 0)?
footpickle footpickle

2020/3/5

#
Just realised I had no brackets in my If statement... Whoops.
You need to login to post a reply.