Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate JMonkey Tutorial to JRuby

I've got all the tutorials up to beginner #5 translated and working, but I don't know Java well enough to know how to port the lines:

private ActionListener actionListener = new ActionListener() {
  public void onAction(String name, boolean keyPressed, float tpf) {
    if (name.equals("Pause") && !keyPressed) {
      isRunning = !isRunning;
    }
  }
};

private AnalogListener analogListener = new AnalogListener() {
  public void onAnalog(String name, float value, float tpf) {
    ...
  }
}

How might this work?

like image 499
Jwosty Avatar asked Jun 11 '26 02:06

Jwosty


1 Answers

As described in Calling Java from JRuby, you can use closure conversion, where blocks can be used to define a Java interface behaviour. Something like the following should work:

l = lambda { |name, pressed, tpf| running = !running if name == 'Pause' && !pressed }
input_managers.add_listener(l, ['Left', 'Right', 'Rotate'])
like image 59
Sébastien Le Callonnec Avatar answered Jun 14 '26 06:06

Sébastien Le Callonnec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!