Cocos2D (6) ---- CCLayer, cocos2d ---- cclayer

Source: Internet
Author: User

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
  1. 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
  1. -(Void) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event;

2> when your fingers move on the screen

[Java]View plaincopy
  1. -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event;

3> when a single finger leaves the screen

[Java]View plaincopy
  1. -(Void) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event;

4> when the touch is canceled

[Java]View plaincopy
  1. -(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
  1. -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {
  2. // Obtain the touch object
  3. UITouch * touch = [touches anyObject];
  4. // Obtain the position of the touch in the UIView.
  5. CGPoint uiPoint = [touch locationInView: touch. view];
  6. // Convert to OpenGL coordinates
  7. CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint];
  8. }


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
  1. // Layer init Method
  2. -(Id) init
  3. {
  4. If (self = [super init]) {
  5. // Initialize an genie
  6. CCSprite * lufy = [CCSprite spriteWithFile: @ "lufy.png"];
  7. CGSize size = [[CCDirector shareddire] winSize];
  8. Lufy. position = ccp (size. width * 0.5f, size. height * 0.5f );
  9. // Add the genie and set the flag
  10. [Self addChild: lufy z: 0 tag: kLufyTag];
  11. Self. isTouchEnabled = YES;
  12. }
  13. Return self;
  14. }

The next step is to receive the touch input in the layer.

[Java]View plaincopy
  1. // Calculate the position of the touch in the layer (OpenGL coordinates)
  2. -(CGPoint) locationInLayer :( NSSet *) touches {
  3. // Obtain the touch object
  4. UITouch * touch = [touches anyObject];
  5. // Obtain the position of the touch in the UIView.
  6. CGPoint uiPoint = [touch locationInView: touch. view];
  7. // Convert to OpenGL coordinates
  8. CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint];
  9. Return glPoint;
  10. }
  11. // Because the practices in ccTouchesBegan, ccTouchesMoved, and ccTouchesEnded are the same, draw a method
  12. -(Void) dealTouches :( NSSet *) touches {
  13. // Calculate the touch position
  14. CGPoint point = [self locationInLayer: touches];
  15. // Obtain the genie Based on the tag
  16. CCSprite * lufy = (CCSprite *) [self getChildByTag: kLufyTag];
  17. // Set the location of the genie
  18. Lufy. position = point;
  19. }
  20. -(Void) ccTouchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
  21. [Self dealTouches: touches];
  22. }
  23. -(Void) ccTouchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {
  24. [Self dealTouches: touches];
  25. }
  26. -(Void) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {
  27. [Self dealTouches: touches];
  28. }

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
  1. 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
  1. -(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration {
  2. // Typedef double UIAccelerationValue;
  3. UIAccelerationValue x = acceleration. x;
  4. UIAccelerationValue y = acceleration. y;
  5. UIAccelerationValue z = acceleration. z;
  6. // X, y, and z represent the acceleration in any direction in 3D
  7. }


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
  1. // RED: # ffff0000
  2. CcColor4B color = ccc4 (255, 0, 0,255 );
  3. // Initialize a color layer
  4. CCLayerColor * layerColor = [CCLayerColor layerWithColor: color];
  5. // Add the scene
  6. [Scene addChild: layerColor];
:


CCLayerGradient

CCLayerGradient is a subclass of CCLayerColor. You can set gradient for layers.

[Java]View plaincopy
  1. // RED: # ffff0000
  2. CcColor4B red = ccc4 (255, 0, 0,255 );
  3. // Blue: # ff0000ff
  4. CcColor4B blue = ccc4 (0, 0,255,255 );
  5. // Initialize a gradient layer from red to blue
  6. CCLayerGradient * layerGradient = [CCLayerGradient layerWithColor: red fadingTo: blue];
  7. // Add the scene
  8. [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
  1. // Create two layers
  2. CCLayer * layer1 = [CCLayer node];
  3. CCLayer * layer2 = [CCLayer node];
  4. // Create a multi-layer, including layer1 and layer2
  5. CCLayerMultiplex * plex = [CCLayerMultiplex layerWithLayers: layer1, layer2, nil];
  6. // Make layer1 active (layer2 is still in memory)
  7. [Plex switchTo: 0];
  8. // Make layer2 active (layer1 is still in memory)
  9. [Plex switchTo: 1];
  10. // Release the currently active layer2 (layer2 is removed from the memory), and then make layer1 active
  11. [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.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.