clip or mask

140 views
Skip to first unread message

Philippe

unread,
Feb 7, 2012, 4:04:22 AM2/7/12
to cocos2d discuss
Hello,

Is there a way to clip a cocos2d Sprite ? (or mask, same thing, but
different word).

thank you,
Philippe

Philippe

unread,
Feb 8, 2012, 11:16:24 AM2/8/12
to cocos2d discuss
Is this the best way to do it ?


class MySprite(Sprite):

def __init__(self):
super(MySprite, self ).__init__('my_sprite.png')
self.gap = 100

def draw(self):
pyglet.gl.glScissor(self.x-self.width/2, self.y-self.height/2,
self.width - self.gap, self.height)
pyglet.gl.glEnable(pyglet.gl.GL_SCISSOR_TEST)
super(MySprite, self ).draw()
pyglet.gl.glDisable(pyglet.gl.GL_SCISSOR_TEST)

Richard Jones

unread,
Feb 8, 2012, 6:12:12 PM2/8/12
to cocos-...@googlegroups.com
Without resorting to shaders or very complex scene composition that's
the only way to do it.


Richard

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

Philippe

unread,
Feb 9, 2012, 2:05:47 AM2/9/12
to cocos2d discuss
It works well.
I will probably write a Sprite class that implement a mask if needed.
thank you!

On Feb 9, 12:12 am, Richard Jones <r1chardj0...@gmail.com> wrote:
> Without resorting to shaders or very complex scene composition that's
> the only way to do it.
>
>      Richard
>

Philippe

unread,
Feb 13, 2012, 9:43:44 AM2/13/12
to cocos2d discuss
Hello again,

Is there a way to make it (mask) works with cocos's Layer ?
something like

class Layer1(Layer):

is_event_handler = True

def __init__(self, png):
super(Layer1, self ).__init__()
self.map = Sprite(png)
self.map.position = self.map.width/2, self.map.height/2
self.add(self.map)

def on_mouse_press(self, x, y, buttons, modifiers):
x, y = director.get_virtual_coordinates (x, y)
if self.map.contains(*self.parent.point_to_local((x, y))):
self.map.x += 10

def draw(self):
pyglet.gl.glScissor(10, 10, 300, 200)
pyglet.gl.glEnable(pyglet.gl.GL_SCISSOR_TEST)
super(Layer1, self ).draw()
pyglet.gl.glDisable(pyglet.gl.GL_SCISSOR_TEST)

This does not work.

Should I use another type of Layer ?
No way to make it work ?

claudio canepa

unread,
Feb 13, 2012, 4:18:10 PM2/13/12
to cocos-...@googlegroups.com
On Mon, Feb 13, 2012 at 11:43 AM, Philippe <philipp...@gmail.com> wrote:
Hello again,

Is there a way to make it (mask) works with cocos's Layer ?
something like

class Layer1(Layer):

   is_event_handler = True

   def __init__(self, png):
       super(Layer1, self ).__init__()
       self.map = Sprite(png)
       self.map.position = self.map.width/2, self.map.height/2
       self.add(self.map)

   def on_mouse_press(self, x, y, buttons, modifiers):
       x, y = director.get_virtual_coordinates (x, y)
       if self.map.contains(*self.parent.point_to_local((x, y))):
           self.map.x += 10

   def draw(self):
       pyglet.gl.glScissor(10, 10, 300, 200)
       pyglet.gl.glEnable(pyglet.gl.GL_SCISSOR_TEST)
       super(Layer1, self ).draw()
       pyglet.gl.glDisable(pyglet.gl.GL_SCISSOR_TEST)

This does not work.

Should I use another type of Layer ?
No way to make it work ?


I think you want to override .visit() and not .draw()

Look at both methods in cocosnode.py

--
claudio

 

claudio canepa

unread,
Feb 13, 2012, 4:30:13 PM2/13/12
to cocos-...@googlegroups.com
Also, ScrollingManager, a subclass of Layer, uses scissors to limit the drawing (look for the parameter viewport). If you are going to do scrolling, you can use the scissors included, or look at the code as scissor example.

--
claudio

Philippe

unread,
Feb 14, 2012, 5:03:49 AM2/14/12
to cocos2d discuss
.visit() is perfect !
thanks again for your help.

I will check later the advantage of using the scrollingManager.


On 13 fév, 22:30, claudio canepa <ccanep...@gmail.com> wrote:
> On Mon, Feb 13, 2012 at 6:18 PM, claudio canepa <ccanep...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages