Cocos2D (6) ---- CCLayer, cocos2d ---- cclayer
A game can have many scenes, and each scene may contain multiple layers. The layers here are generally CCLayer objects. CCLayer has almost no functions. Compared with CCNode, CCLayer can be used to receive touch and accelerator inputs. In fact, cocos2d does not have strict requirements on the layer. The layer does not have to use the CCLayer class. It can also be a simple CCNode. Why? Instead of creating a layer to accommodate more subnodes, CCNode can also be used to add subnodes. Therefore, if your layer does not need to receive the touch and accelerator input, use CCNode to represent the layer. Because it can receive the touch and accelerator input, it will increase unnecessary overhead. Move, zoom, and rotate the entire layer. All nodes on the layer also move, zoom, and rotate together.
Common settings
1. Receive touch Input
By default, CCLayer does not receive touch input. You need to set isTouchEnabled to YES
[Java]View plaincopy
- Self. isTouchEnabled = YES;
After you set isTouchEnabled to YES, the corresponding method of the layer is called to process the touch input:
These are all methods defined in the CCStandardTouchDelegate protocol.
1> when a single finger is exposed to the screen
[Java]View plaincopy
- -(Void) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event;
2> when your fingers move on the screen
[Java]View plaincopy
- -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event;
3> when a single finger leaves the screen
[Java]View plaincopy
- -(Void) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event;
4> when the touch is canceled
[Java]View plaincopy
- -(Void) ccTouchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event;
The touch is rarely canceled, so it can be ignored in most cases, or replaced with ccTouchesEnded, because ccTouchesCancelled and ccTouchesEnded are similar.
In most cases, we need to know where the touch occurs. The Touch event is received by the UIKit framework. Therefore, you need to convert the touch position to OpenGL coordinates.
For example, during finger movement:
[Java]View plaincopy
- -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {
- // Obtain the touch object
- UITouch * touch = [touches anyObject];
- // Obtain the position of the touch in the UIView.
- CGPoint uiPoint = [touch locationInView: touch. view];
- // Convert to OpenGL coordinates
- CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint];
- }
The following uses a small example to combine the above method. Suppose there is an genie on the layer, where I touch my fingers, and where the genie is located.
First, add the genie during layer initialization.
[Java]View plaincopy
- // Layer init Method
- -(Id) init
- {
- If (self = [super init]) {
- // Initialize an genie
- CCSprite * lufy = [CCSprite spriteWithFile: @ "lufy.png"];
- CGSize size = [[CCDirector shareddire] winSize];
- Lufy. position = ccp (size. width * 0.5f, size. height * 0.5f );
- // Add the genie and set the flag
- [Self addChild: lufy z: 0 tag: kLufyTag];
- Self. isTouchEnabled = YES;
- }
- Return self;
- }
The next step is to receive the touch input in the layer.
[Java]View plaincopy
- // Calculate the position of the touch in the layer (OpenGL coordinates)
- -(CGPoint) locationInLayer :( NSSet *) touches {
- // Obtain the touch object
- UITouch * touch = [touches anyObject];
- // Obtain the position of the touch in the UIView.
- CGPoint uiPoint = [touch locationInView: touch. view];
- // Convert to OpenGL coordinates
- CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint];
- Return glPoint;
- }
- // Because the practices in ccTouchesBegan, ccTouchesMoved, and ccTouchesEnded are the same, draw a method
- -(Void) dealTouches :( NSSet *) touches {
- // Calculate the touch position
- CGPoint point = [self locationInLayer: touches];
- // Obtain the genie Based on the tag
- CCSprite * lufy = (CCSprite *) [self getChildByTag: kLufyTag];
- // Set the location of the genie
- Lufy. position = point;
- }
- -(Void) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
- [Self dealTouches: touches];
- }
- -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {
- [Self dealTouches: touches];
- }
- -(Void) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {
- [Self dealTouches: touches];
- }
The layer touch input is now mentioned here. Other advanced usage will be mentioned later.
2. Receive the accelerator Input
By default, CCLayer does not receive the accelerator input. You must set isAccelerometerEnabled to YES explicitly.
[Java]View plaincopy
- Self. isAccelerometerEnabled = YES;
When isAccelerometerEnabled is set to YES, the corresponding method of the layer is called to process the accelerator input:
This is the method defined in the UIAccelerometerDelegate protocol.
[Java]View plaincopy
- -(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration {
- // Typedef double UIAccelerationValue;
- UIAccelerationValue x = acceleration. x;
- UIAccelerationValue y = acceleration. y;
- UIAccelerationValue z = acceleration. z;
- // X, y, and z represent the acceleration in any direction in 3D
- }
CCLayerColor
Sometimes we want to set a background color for the entire layer, so we need to use CCLayerColor. CCLayerColor is a subclass of CCLayer.
[Java]View plaincopy
- // RED: # ffff0000
- CcColor4B color = ccc4 (255, 0, 0,255 );
- // Initialize a color layer
- CCLayerColor * layerColor = [CCLayerColor layerWithColor: color];
- // Add the scene
- [Scene addChild: layerColor];
:
CCLayerGradient
CCLayerGradient is a subclass of CCLayerColor. You can set gradient for layers.
[Java]View plaincopy
- // RED: # ffff0000
- CcColor4B red = ccc4 (255, 0, 0,255 );
- // Blue: # ff0000ff
- CcColor4B blue = ccc4 (0, 0,255,255 );
- // Initialize a gradient layer from red to blue
- CCLayerGradient * layerGradient = [CCLayerGradient layerWithColor: red fadingTo: blue];
- // Add the scene
- [Scene addChild: layerGradient];
:
CCLayerMultiplex
CCLayerMultiplex inherits from CCLayer, which is called "multi-layer ". It can contain multiple CCLayer objects, but only one CCLayer can be active at any time. switchTo: And switchToAndReleaseMe: can be used to make a layer active. The difference is that switchToAndReleaseMe: the method first releases the currently active layer, and then the Layer required by the parameter is in the active state.
[Java]View plaincopy
- // Create two layers
- CCLayer * layer1 = [CCLayer node];
- CCLayer * layer2 = [CCLayer node];
- // Create a multi-layer, including layer1 and layer2
- CCLayerMultiplex * plex = [CCLayerMultiplex layerWithLayers: layer1, layer2, nil];
- // Make layer1 active (layer2 is still in memory)
- [Plex switchTo: 0];
- // Make layer2 active (layer1 is still in memory)
- [Plex switchTo: 1];
- // Release the currently active layer2 (layer2 is removed from the memory), and then make layer1 active
- [Plex switchToAndReleaseMe: 0];
Switching between layers has no effect on transition.
Address: http://blog.csdn.net/q199109106q/article/details/8601533
Thank you ~!
Why cannot a CCLayer created in COCOS2d be displayed?
Have you added it?
How to jump from sdk (UIController) to cocos2d (CClayer)
This is not the case yet. You can introduce the 2d library directly in the project.