Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@inger
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inger/07a1ce0973b41bdc3946 to your computer and use it in GitHub Desktop.
Save inger/07a1ce0973b41bdc3946 to your computer and use it in GitHub Desktop.
## jruby-users question "Overriding A Method For A Specific Instance" -
####### See: http://markmail.org/thread/emm5h6psqi4z3leq
##
#### Start this with jruby -rgdx.jar gdx_listener_hack.rb
###
#### where gdx is coming from http://libgdx.badlogicgames.com/releases/libgdx-1.0.1.zip
java_import "com.badlogic.gdx.scenes.scene2d.InputListener"
############### from email:
def listener_for(method, &block)
Class.new(InputListener) do
define_method(method, &block)
def initialize(mgr)
super()
@mgr = mgr
end
def create_method(name, &inner_block)
self.class.send(:define_method, name, &inner_block)
end
end.new(@mgr)
end
@inv_listener = listener_for(:touchDown) do |event, x, y, pointer,
button|
puts "starboard"
true
end
@inv_listener.create_method(:touchUp) do |event, x, y, pointer,button|
puts "stankboard"
true
end
# I can see that the method is being created by calling it:
#### Testing
puts "Calling touchUp..."
@inv_listener.touchUp(nil,nil)
puts "Calling touchDown..."
@inv_listener.touchDown(nil,nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment