On Sat, Aug 18, 2012 at 5:05 PM, Nitneroc <[email protected]> wrote:

> After reading those threads, I tried another way to "cut" a sprite (ie
> only display some part of it) :
>
> https://groups.google.com/forum/?fromgroups#!topic/cocos-discuss/Ks83VdBj9Wk%5B1-25%5D
>
>
> https://groups.google.com/forum/?fromgroups#!topic/cocos-discuss/vUZOwNZW_GY%5B1-25%5D
>
>
> Here's what I came up with for cutting the top of a sprite (the only thing
> I'm actually interested in) :
>
> class Cut_Sprite(cocos.sprite.Sprite):
>
>     def __init__(self,*args,**kwargs):
>         super(Cut_Sprite,self).__init__(*args,**kwargs)
>         self.tex_scale = self.width/self._vertex_list.tex_coords[3] #used
> to convert pixels to texture coords
>
>     def cut(self,desired_height):
>         desired_height=int(desired_height)#needs int
>         self._vertex_list.vertices[5]=self.y+desired_height
>         self._vertex_list.vertices[7]=self.y+desired_height
>         self._vertex_list.tex_coords[7]=desired_height/self.tex_scale
>         self._vertex_list.tex_coords[10]=desired_height/self.tex_scale
>
> What do you think of such a solution ? I'm a newbie, I don't know anything
> about openGL, so I guess there are other/better solutions (some are
> mentionned in the threads I mentionned earlier), but this one works both
> inside and outside a batch.
> here's a small test-app : http://pastebin.com/xmn0KERe
>


The big problem is that you modify the vertex_list in parallel with other
code in Sprite:
if you change position, scale, rotation or image after the creation, the
vertex list will be changed by other code in Sprite and it will not look as
a cut sprite.

The proper general way to implement this should be:
   1. add a property desired_height to sprite
   2. make the setter for this property call _update_position
   3. modify _update_position to account for desired_height when rebuilding
the vertex list, and also add the texcoord change there

Another way to attack the problem is thinking one level up: a cluster of
textured quads.
Suitability for the task depends on the use case, which I don't know.
The implementation can be based on the ParticleSystem code.
The advantage is that theres no long chain of classes interacting,

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