Class: Rubygame::EventActions::MethodAction

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygame/event_actions.rb

Overview

MethodAction is an event action used with EventHook. MethodAction takes a symbol giving the name of a method. When it is performed, it calls that method on the owner, passing it the event that triggered the hook.

Example:

class Player
  def aim_at( event )
    self.crosshair_pos = event.pos
  end
end

player1 = Player.new

EventHook.new( :owner   => player1,
               :trigger => MouseMoveTrigger.new(),
               :action  => MethodAction.new( :aim_at ) )

Instance Method Summary collapse

Constructor Details

#initialize(method_name) ⇒ MethodAction

Create a new MethodAction using the given method name.

method_name

the method to call when performing. (Symbol, required)



146
147
148
# File 'lib/rubygame/event_actions.rb', line 146

def initialize( method_name )
	@method_name = method_name
end

Instance Method Details

#perform(owner, event) ⇒ Object

Call the method of the owner represented by @method_name, passing in the event as the only argument.

If that causes ArgumentError (e.g. because the method doesn’t take an argument), calls again without any arguments.

If that also fails, this method re-raises the original error.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rubygame/event_actions.rb', line 159

def perform( owner, event )
	owner.method(@method_name).call( event )
rescue ArgumentError => s
	begin
		# Oops! Try again, without any argument.
		owner.method(@method_name).call()
	rescue ArgumentError
		# That didn't work either. Raise the original error.
		raise s
	end
end