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

2017/8/13

bullettimer help

mole2003 mole2003

2017/8/13

#
whenever i fire my bullet the bullettimer goes to ten (as planned) but then goes to 9 and just stops. Any ideas about what is wrong and where could i fix it?
public void fire()
    {
      getWorld().addObject( new bullet(), getX(), getY());
      bullettimer = bullettimer + 10;
      if (bullettimer > 0)
      {
        bullettimer = bullettimer - 1;  
      }
      getWorld().showText(Integer.toString(bullettimer), 10, 10);
    }
mole2003 mole2003

2017/8/13

#
just thought i would add that the bullet will only fire if the space key is down and the bullettimer = 0.
EduandErnst EduandErnst

2017/8/13

#
What you're doing is calling the method fire() next up you're assigning the value of "bullettimer +10" to the variable bullettimer and then your calling an if statement that reduces one from your variable bullettimer The problem is, whenever you are calling the method fire(), you add 10 to your vaiable bullettimer (which by the way should be called "bulletTimer") what your probably trying to do is a loop?:
while(bulletTimer > 0){
bulletTimer--;
}
danpost danpost

2017/8/13

#
You have the code that runs the timer (lines 5 through 8) in the 'fire' method which is probably only being called when the value of the timer is zero. Either move the running of the timer outtside the method or put the condition to fire within the method and call the method unconditionally.
mole2003 mole2003

2017/8/18

#
thanks alot
You need to login to post a reply.