AnalogListener variables: speed and value

Hi all,

I’m following the beginner tutorials and I’m a little confused by tutorial 5 - Hello Input System:

Within the AnalogListener code:
[java] private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (isRunning) {
if (name.equals(“Rotate”)) {
player.rotate(0, value*speed, 0);
}
// snip
}
}
};[/java]

Firstly, it seems that we are using a variable speed without defining it ourselves or accessing it as a member of another class - e.g. app.speed or app.getSpeed() - How is this even possible?

Secondly, the JavaDoc for ActionListener states “The results of KeyTrigger and MouseButtonTrigger events will have tpf == value.”, but supposing I want to let the player control a character with both a joystick and/or the cursor keys, and as such I have added “Up”, “Down”, “Left” and “Right” mappings for both control methods at the inputManager. Am I right in thinking that value changes from 0 to 1 as I increase a direction on the joystick, such that the correct code for rotating the character would include value*tfp - e.g. with the left/right joystick axis input set to +0.5 the character would rotate at half-speed (and tpf would ensure this rotation speed is consistent on all systems) … surely then KeyTrigger and MouseButtonTrigger events should have value==1.0f such that the same code continues to function when a key is pressed to rotate the character? (rotating the character at full speed when pressed or not at all when released).

speed is an instance variable of Application, and as you are using it in a derived SimpleApplication class (which extends Application), it implicitly calls this.speed for you.

Value is frame independent already

As for KeyInputs in an analog listener, its so that you can hold it down, and execute something as long as it is being pressed and have access to tpf.

1 Like

Ah, that all makes a lot of sense, thank you.

I guess I just expected value to be unadjusted for frame rate in case anyone wanted to show the unadjusted input. for example, a steering wheel on the HUD in a racing game where a joystick input of +0.5 would result in the image being rotated half-way to full lock (of course, this could still be done by dividing value by tpf with the current method).

I would totally suggest to you read proper material and not only tutorials on Java first before attempt to do game programming. There are quite a lot of books in this topic.