OK, so I’m trying to make a reloading and shooting statement using the if, and I currently have it set up so if you click, it decrements the integer “ammo” and displays is as a bitmap text. however, when i click and hold, it decreases far too fast, and a clip of 30 shots is gone in about a second. Is there any way to tell it to only fire once or less often? Here’s the code:
[java] AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) {
if (name.equals(“shoot”)) {
ammo–;
}
if (name.equals(“reload”)) {
ammo = 30;
mag = mag - 1;
}
ammoReadout.setText("Ammo: " + ammo + "n Mags: " + mag);
if (ammo <= 0) {
ammoReadout.setText(“Press R to reload”);
}
if (mag <= 0) {
ammoReadout.setText(“Out of ammo!”);
}
}
};[/java]
I think you can use the tfp parameter to tell, how much time elapsed since the previous call to the listener…
Thanks, but where would I change it, and what unit is it measured in? Like this?
[java]if (name.equals("shoot")) {
ammo–;
tpf = 20f;
}[/java]
Thanks so far!
you should do something like:
[java]
private float elapsedTime = 0;
private float shootFrequency = 1.20f;
AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) {
elapsedTime += tpf;
if (name.equals("shoot") && elapsedTime > shootFrequency) {
ammo–;
elapsedTime = 0;
}
[…snip…]
}
};
[/java]
Thanks, it worked!