Parent

Methods

Class/Module Index [+]

Quicksearch

Rubygame::EventActions::MethodAction

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 ) )

Public Class Methods

new( method_name ) click to toggle source

Create a new MethodAction using the given method name.

method_name

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

# File lib/rubygame/event_actions.rb, line 146
def initialize( method_name )
        @method_name = method_name
end

Public Instance Methods

perform( owner, event ) click to toggle source

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.

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.