I have seen @noone s answer and it is correct. I just answer you, because i had to add the same to my game. What i did: I had a variable boolean attacking
, which stores if you are holding firebutton. Then i had 2 more variables: float bps
, which stores how many bullets you can shoot per second and float reloadTime
, which stores how long it takes to reload an empty magazin, if you use one. I also store a long time
and a boolean reloading
. Time stores the TimeUtils.millis()
of your last shot, the reloading stores if you are reloading the magazin or just shooting. Then in the attack method i call a method: public boolean readyToAttack()
, in which i compare TimeUtils.millis()
to my variable time
. If reloading = true
, TimeUtils.millis() - reloadTime
has to bigger then time
. If not, TimeUtils.millis() - (1000 / bps)
has to be bigger then time
. If this is the case the method returns true and i can shoot. Noones solution is simpler, but for me bps is easier to understand so i used this.
Hope it helps