On Sat, Sep 25, 2010 at 1:14 AM, Tim <[email protected]> wrote:

> Hey all. Was looking over some of the sample code, and I see that you
> can listen for mouse events for layers, but can you also do this for
> other objects?
>
> In short, what I'm asking, is how would you go about testing to see if
> an on-screen object (such as a sprite) has been clicked? I've gotten
> some code that calculates what is under the mouse pointer at the point
> of the click, but it seems like this code would be better fit into the
> object it is a part of, as opposed to the game logic.
>
> Any tips/ideas are both welcome and appreciated.
>
> --
>


yes, you can receive events at any CocosNode.
Mouse events are published by the window, so you will need to register with
the window.
By example, if you want a sprite to get the mouse events:

class SpriteMouseListener(cocos.sprite.Sprite):
   def on_enter(self):
      super(SpriteMouseListener, self).on_enter()
      director.window.push_handlers(self.on_mouse_press)

   def on_exit(self):
      director.window.pop_handlers()
      super(SpriteMouseListener, self).on_exit()

   def on_mouse_press(self,x,y,button,modifiers):
       your code

--
claudio

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cocos-discuss?hl=en.

Reply via email to