Original address http://blog.csdn.net/jhonlight/article/details/38408351
Today, a group of friends asked me:
I put a layer in scene, and the layer holds an image, that is, the background image. I want to change this image and require the effect of switching between scene and scene provided by the system, so I used the director class to switch the scene of the animation package provided by the system, but the effect was that the entire scene was switched, and what I wanted was only the background change, the rest remain unchanged.
As we all know, this is certainly not acceptable. The scctor (Director) switch will switch the established scene, and the scene will replace the previous scene container. In this case, what should we do? At this time, I thought about the grid effect. Since you want to fade in and out, the grid effect is quite good. So he did what I said and later found that the effect of the grid only fades out of fadeoutbltiles. What should I do? Don't forget that cocos2dx has such an action,Reverse (); that is, after fading out, can we not be understood as fading out? Yes, it basically meets the requirements.
The above is just an episode. The following uses a small example to briefly describe the use of grid effects.
First, let's take a look at the official website.
Grid actions are similar to special effects to achieve flip, jitter, shock, water ripple, and other effects. In the Cocos2D-x, the base class ccgridbase of the grid class has two subclasses, ccgrid3d and cctiledgrid3d. The two classes share the same thing: each sub-block of the grid can be separated. The inheritance relationship of ccgridbase is as follows:
Ccgrid3d, cctiledgrid3d --> ccgridbase --> ccobject.
There is no direct application of the grid, as long as you understand ccgrid3d and cctiledgrid3d, And the grid action is based on the grid. Nodes running grid actions seem to be divided into many rectangles of the same size. The actions of these rectangles form an overall action, and these rectangles form a matrix. The 16x12 mesh runs very fast, but the effect is not very good. The 32x24 mesh looks great, but sometimes it doesn't run too fast.
Note: Before using grid effects, you need to disable the deep test of OpenGL.
As to why we should disable the deep test, I personally think that the grid effect is to split the plane of the node and its subclass into various grids for specific special effects. Since it is flat, there is no need for in-depth detection. 1 will affect the effect, and 2 will consume additional performance if you enable the deep test. This is my personal understanding. If you have any mistakes, please make corrections.
OK. Check the effect before running the code.
You can use an image to enlarge the position of a touch point to display the effect.
1. First, the header file is defined as "Touch start", "Touch Move", and "Touch End". Three Methods and private variables of a grid instance are defined.
Initialize the grid effect instance and add it to the current layer. Next, place a genie in the grid instance. we can regard the grid effect instance as a container, it will make the scaled object produce the specified special effect.
2. initialize the touch listener and callback method. I use cocos2dx3.2, which is different from the touch before 3.0. If you are not familiar with this method, go to the official documentation.
3. First, use pevent to obtain the touch target and define a rectangular area. Then, determine whether the current vertex is in this rectangular area. If false is returned, it indicates that the touch is an genie, otherwise, the touch points are outside the genie.
4. Define the effect of a method and let the previously initialized grid instance run this special effect action.
5. The method for moving a touch is similar to that for ending a touch.
6. Running OK is the starting effect.
7. If you are interested in other special effects, try them one by one. The results are quite good.