0

I have a problem and I saw it also in the game Candy Crush Saga, where they successfully dealt with it. I would like the sprite to show only when it is in the board (see image link below). The board can be of different shapes, like the levels in the mentioned game. Has anyone some ideas how can this be achieved with Cocos2d? I will be very glad if someone has some tips.

Thank you in advance.

image link: http://www.android-games.fr/public/Candy-Crush-Saga/candy-crush-saga-bonus.jpg

svarad
  • 21
  • 6

2 Answers2

1

In Cocos2d you can render sprites at different z levels. Images at a lower z-level will be drawn first by the graphic card and the images (sprites) with a higher z-value will be drawn later. Hence if an image (say A) is at the same position of the other but has a higher z-value you will see only the pixels of image A where the two images intersect.

Cocos2d uses also layers so you can decide to add Sprites to a layer and set the layer to a specific z value. I expect that they used a layer for the board (say at z=1) with a PNG image containing transparent bits in the area where you can see the sprites, and a second layer at z=0 for the sprites. In this way you can only see the sprites when they are in the transparent area.

Does this help?

mm24
  • 9,280
  • 12
  • 75
  • 170
  • I' am familiar with Cocos2d. I already thought of similar solution, but this does not solve the problem. The board in the game is half transparent, so if the sprites have z index 0 they would not be so visible. There must be another solution, I am thinking about the stencil buffer. – svarad Sep 05 '13 at 10:04
  • Try a simple solution with layers first and then worry about other techniques. I never played Candy Crush but that's what it seem to me. You need layers anyway for a good code structure :). – mm24 Sep 06 '13 at 09:03
0

I found out Cocos2d has a class CCClippingNode which does exatclly what I wanted. First I thought it can clip only rectangular areas, but after some research I found it can clip also paths.

svarad
  • 21
  • 6