tags:

views:

191

answers:

4

The is any way to remove the events in Shoes? I searched around many websites and references but I can't found any way to remove a event... I minding that I will need to create my own event manager... its really nescessary? or there is a way to desattach event listeners?

A: 

Have you tried method.unbind(obj)?

Max Gontar
That will give him a copy of the method that isn't bound to the object, but it won't remove the method from the object, and I can't see any interpretation of the question for which that would help.
MarkusQ
A: 

Can you clarify what you mean by "remove a event"? Are you wanting to ignore a specific event or are you wanting to "turn off" an event listener?

If the former, I'd suggest writing a listener that just ignore the event.

If the later, why not make the body of the listener conditional on some externally accessible value, giving yourself an on/off switch.

If you are wanting something else, edit the question to clarify and I'll stop back later and edit my answer.

In response to your comment, I'd re-suggest the second of the above alternatives. If you wanted to get really fancy you could write something like this:

$keypress_listeners = {}
keypress do |key|
    $keypress_listeners.values.each { |l| l.call(key)
    end
$keypress_listeners[:hero_controller] = lambda { |key| ... }
:
:
$keypress_listeners.delete[:hero_controller]

and likewise for any other events, but that is probably overkill. On the other hand, it would give you total control of the event processing.

MarkusQ
The case is, i'm creating a game, and when the game enters in a level, i need to add events for controlling the hero, and when the game left the game (for going to another screen type) i need to remove events that controls the hero, to add then left when enter in a new stage
+1  A: 

Which keybindings do you want to remove? All events on the Shoes app or just the default bindings?

If you want to override bindings reserved for shoes like "alt-/", "alt-.", "alt-?", paste the following code into the file which contains your application code

class Shoes::App
  def Shoes.show_log # gets called on alt-/
  end

  def Shoes.show_manual # gets called on alt-?
  end

  def Shoes.show_selector # gets called on alt-.
  end
end

The above code monkey-patches the shoes code and in-turn does nothing when the corresponding keys are pressed.

You can use the same technique for rest of the default bindings. grep the shoes source for the key bindings, find the corresponding method and define an empty method within your app to override the built-in method.

Abhijith
+2  A: 

I have found that Shoes only allows a single listener method on a given event, so you can remove a previous listener by calling the event and not passing a block to it.

For example, this Shoes app will clear the click event after it is clicked one time. If you remove the call to click inside the block, then it will fire repeatedly.

Shoes.app
  @p = para "Empty"
  @click_count = 0
  click do |b,x,y| 
    @click_count += 1
    @p.replace "Clicked #{@click_count} time(s)."
    click # remove the click handler
  end
end
Jonathan Branam