IOS_31_cocos2d _ layer CCLayer _ accelerator
Finally:
In the cocos2d-x, the inheritance structure of the Layer:
From the figure above, we can see that:
Which of the following direct subclasses are: Control, ScrollView, Menu, and LayerColZ labels? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/J0tTIw828suO + keys/uPazob6wwO/keys + keys/ydPD09q908rVtKXD/ authorization + warning/warning + qz/warning/7Cgo8c3Ryb25nPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140928/2014092810370590.png" alt = "\">
Note:
In cocos2d v3, because CCLayer is no longer used, Scene
Compliance Agreement After,
You can directly
Listen to the accelerator events
,
When the mobile phone is vertical, the acceleration value in the x direction is approximately 0.
When the mobile phone is tilted to the right, the acceleration value in the x direction is increased.
When the mobile phone is completely tilted to the right, the acceleration value in the x direction is approximately equal to + 1.0
When the mobile phone is tilted to the left, the acceleration value in the x direction is reduced.
When the mobile phone is completely tilted to the left, the acceleration value in the x direction is approximately-1.0
As shown in the following code:
/// AccelerometerScene. m // 31_cocos2D getting started /// Created by beyond on 14-9-27. // Copyright (c) 2014 com. beyond. all rights reserved. // implementation: the mobile phone is tilted to the right, and the nanaSprite in the center moves to the right; the mobile phone is tilted to the left, and the nanaSprite in the center moves to the left; # import "AccelerometerScene. h "// to listen for events on the accelerator, you must comply with the Protocol
@ Interface AccelerometerScene ()
{// Principle: Record accelerameterX in the listening accelerator method, and change the sprite's Position CGFloat _ accelerationX in the clock method update ;} @ end @ implementation AccelerometerScene # pragma mark-Override parent class method-(id) init {if (self = [super init]) {// 1. Scenario Node allows interaction with self. userInteractionEnabled = YES;} return self;} // method for implementing the parent class, add a button to the screen-(void) addShowBtns {}# pragma mark-the accelerator agent method // The scenario must first comply with the Protocol CCAccelerometerDelegate // unlike the touch event, the accelerator event has only one method, namely didAccelerate-(void) acce Lerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration {// The range is-1 ~ 1. For example, in the mobile phone vertical state, x is tilted to the right. x is gradually increased from 0 to + 1 // x. in the mobile phone vertical state, x is skewed to the left, x gradually decreases from 0 to-1 CCLOG (@ "x = % f, y = % f, z = % f", acceleration. x, acceleration. y, acceleration. z); // sensitivity int lingmindu = 6; _ accelerationX = acceleration. x * lingmindu;} // The only note about layer and accelerator: record the value of the accelerator in the listening accelerator method, and, you can only set the position of sprite when refreshing frames. The purpose of this operation is to ensure the smoothness of the image !!! Because of the update method, it can be called 60 times in one second, while the CDN monitoring method calls 10 times in one second-(void) update {self. sprite. position = ccpAdd (self. sprite. position, ccp (6, 0);} @ end
Cocos2d v3 touch event practices:
Set scene Node to allow interaction
Self. userInteractionEnabled = YES;
The usage of cocos2d v3 is as follows:
Common CCLayer settings
1. Receive touch Input
By default, CCLayer does not receive touch input. You need to set isTouchEnabled to YES
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
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
2> when your fingers move on the screen
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
3> when a single finger leaves the screen
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
4> when the touch is canceled
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
The touch is rarely canceled,
Therefore, it can be ignored in most cases, or replaced by ccTouchesEnded,
Because ccTouchesCancelled and ccTouchesEnded are similar
In most cases, you 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:
-(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 the OpenGL coordinate CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint];}
The following uses a small example to combine the above method. Suppose there is a Sprite on the layer, where the finger is touched, and where the Sprite is located.
First, add the genie during layer initialization.
// Layer init method-(id) init {if (self = [super init]) {// initialize a Sprite CCSprite * nana = [CCSprite spriteWithFile: @ "nana.png"]; CGSize size = [[CCDirector shareddire] winSize]; nana. position = ccp (size. width * 0.5f, size. height * 0.5f); // Add the genie and set the flag [self addChild: nana z: 0 tag: kNanaTag]; self. isTouchEnabled = YES;} return self ;}
The next step is to receive the touch input in the layer.
// 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 the OpenGL coordinate CGPoint glPoint = [[CCDirector shareddire] convertToGL: uiPoint]; return glPoint;} // The practices in ccTouchesBegan, ccTouchesMoved, and ccTouchesEnded are the same, therefore, extract a method-(void) dealTouches :( NSSet *) touches {// calculate the position of the touch CGPoint point = [self locationInLayer: touches]; // obtain the genie CCSprite * nana = (CCSprite *) [self getChildByTag: kNanaTag] based on the marker; // set the location of the genie nana. 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];}
Above is the touch input of layers before cocos2d v3
2. Accelerated meter events before coco2d v3
By default, CCLayer does not receive the accelerator input. You must set isAccelerometerEnabled to YES explicitly.
self.isAccelerometerEnabled = YES;
When isAccelerometerEnabled is set to YES, the corresponding method of the layer is called to process the accelerator input:
This is inUIAccelerometerDelegateMethods defined in the Protocol
-(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}
3. CCLayerColor
By default, CCLayer cannot be set to a color,
Sometimes, you want to set a background color for the entire layer,
Therefore, CCLayerColor is required,
CCLayerColor is a subclass of CCLayer.
// RED: # ffff1_cccolor4b color = ccc4 (255, 0, 0,255); // initialize a color layer CCLayerColor * layerColor = [CCLayerColor layerWithColor: color]; // Add [scene addChild: layerColor] to the scene.
:
4. CCLayerGradient
CCLayerGradient is a subclass of CCLayerColor,
You can set gradient for layers.
// Red: # ffff1_cccolor4b red = ccc4 (255, 0, 0,255); // blue: # ff1_ffcccolor4b blue = ccc4 (0, 0,255,255); // initialize a gradient layer, gradient from red to blue CCLayerGradient * layerGradient = [CCLayerGradient layerWithColor: red fadingTo: blue]; // Add [scene addChild: layerGradient] to the scene;
:
5. CCLayerMultiplex
CCLayerMultiplex inherits from CCLayer, which is called "multi-layer ".
It can contain multiple CCLayer objects,
However, only one CCLayer can be active at any time.,
SwitchTo: And switchToAndReleaseMe: Method
Allows a layer to be active,
The difference is that switchToAndReleaseMe: Method
The active layer is released first,
Make the Layer required by the parameter active again
// Create two layers: CCLayer * layer1 = [CCLayer node]; CCLayer * layer2 = [CCLayer node]; // create a multi-layer that includes layer1 and layer2. However, only one of the layers can be displayed at a time. 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 make layer1 active [plex switchToAndReleaseMe: 0];
Switching between layers has no effect on transition.