On Wed, Sep 25, 2013 at 4:09 PM, Paul Pittlerson <[email protected]>wrote:
> Layer.remove_all_handlers does not seem to do what the docs says, unless
> I'm misunderstanding.
>
> This is part of my code:
>
> def activate(self):
>
> self.is_event_handler = True
> self.push_all_handlers()
> self.visible = True
>
> def deactivate(self):
>
> self.remove_all_handlers()
> self.is_event_handler = False
> self.visible = False
>
> def on_mouse_press(self, x, y, b, m):
> if y < director.get_window_size()[1] - 96:
>
> for button in self.tilebuttons:
> if button.contains(x, y):
> return True
>
> return False
>
> From the docs:
>
> remove_all_handlers(self)
>> de-registers itself to receive director.window events and propagates the
>> call to childs that are layers.
>>
>
> Should make it so self.on_mouse_press is not registered, but this is not
> what happens in my code, unless I also use the is_event_handler=False
>
> Have I misunderstood the usage, or is it maybe some other bug in my code?
> The use case is toggling on / off a layer which acts as a kind of menu.
>
>
>
cocos will automatically push the handlers (for a layer or scene) when they
become part of the active scene (the one that director is running). It does
so in the on_enter method of layer / scene.
Your code is doing an additional push, so they are two frames in the pyglet
window EventHandler,that is, double registering the listeners.
(and for each pyglet window event you will receive two calls in your
handlers)
When you call remove_all_handlers, it will remove only one frame of event
handlers, so the other is kept active.
The standard way to connect / disconnect all the layer listeners that want
to be called about ** pyglet.Window events ** is:
Mode1: add / remove pattern
1)
class MyLayer(cocos.layer.Layer):
# your class definition
layer = MyLayer(...)
layer.is_event_handler = True
# The later hint cocos this layer wants to receive events, but it will not
receive events until the layer is into the active scene:
2)
scene = Scene()
scene.add(layer)
# layer still not seeing events: it is into one scene, but that scene is
not active
3)
director.run(scene)
# layer receiving window events because is into the active scene
4)
director.push(other_scene)
# layer not seeing window events because the active scene is other_scene,
not the scene to which layer attached.
5)
director.pop()
# guess
scene.remove(layer)
# layer not more in the any scene, in particular not in the active scene,
so not receiving pyglet.Window events.
Notice that is_handler_active besides allowing/disallowing layer's
listeners , it also allows / block propagation of events to child layers.
By example, if you have
scene
layer 1
layer 2
layer_1.is_event_handling == False
then layer_2 will not see events, irrespective of his own
is_event_handling value.
Misc notes:
if you define custom events they will not be automatically
registered-unregistered. You should register in the on_enter method and
de-registering at the on_exit method
nodes other than layer or scenes don't do automatic registration
--
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/groups/opt_out.