一个游戏中可以有很多个场景,每个场景里面又可能包含有多个图层,这里的图层一般就是CCLayer对象。CCLayer本身几乎没什么功能,对比CCNode,CCLayer可用于接收触摸和加速计输入。其实,cocos2d对图层并没有严格的要求,图层不一定要使用CCLayer类,它也可以是一个简单的CCNode,为什么呢?我们新建一个图层不就是为了能够容纳更多的子节点么,CCNode也可以添加子节点啊。所以,如果你的图层不需要接收触摸和加速计输入,就尽量使用CCNode表示图层,CCLayer因为能够接收触摸和加速计输入会增加不必要的开销。移动、缩放、旋转整个图层,图层上的所有节点也会跟着一起移动、缩放、旋转。
常用设置
1.接收触摸输入
CCLayer默认情况是不接收触摸输入的,需要显示地设置isTouchEnabled为YES
[java] viewplaincopyself.isTouchEnabled = YES; 设置isTouchEnabled为YES后,就会调用图层相应的方法来处理触摸输入:这些都是在CCStandardTouchDelegate协议中定义的方法
1> 当单指接触到屏幕时
[java] viewplaincopy- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 2> 当手指在屏幕上移动时
[java] viewplaincopy- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 3> 当单指离开屏幕时
[java] viewplaincopy- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 4> 当触摸被取消时
[java] viewplaincopy- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 很少会发生触摸被取消的情况,所以大多数情况下可忽略,或用ccTouchesEnded代替,因为ccTouchesCancelled和ccTouchesEnded类似
大部分情况下,我们需要知道触摸发生在什么位置。这里的触摸事件是由UIKit框架接收的,因此需要把触摸位置转换为OpenGL坐标。比如在手指移动过程中:
[java] viewplaincopy- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // 获取触摸对象 UITouch *touch = [touches anyObject]; // 获取触摸在UIView视图上的位置 CGPoint uiPoint = [touch locationInView:touch.view]; // 转换为OpenGL坐标 CGPoint glPoint = [[CCDirector sharedDirector] convertToGL:uiPoint]; }
下面利用一个小例子来综合使用上述的方法,假设图层上有个精灵,我手指触摸到哪,这个精灵的位置就在哪首先在图层初始化的时候添加精灵
[java] viewplaincopy// 图层的init方法 -(id) init { if( (self=[super init])) { // 初始化一个精灵 CCSprite *lufy = [CCSprite spriteWithFile:@"lufy.png"]; CGSize size = [[CCDirector sharedDirector] winSize]; lufy.position = ccp(size.width * 0.5f, size.height * 0.5f); // 添加精灵,并设置标记 [self addChild: lufy z:0 tag:kLufyTag]; self.isTouchEnabled = YES; } return self; }
接下来是在图层中接收触摸输入
[java] viewplaincopy// 计算触摸在图层中的位置(OpenGL坐标) - (CGPoint)locationInLayer:(NSSet *)touches { // 获取触摸对象 UITouch *touch = [touches anyObject]; // 获取触摸在UIView视图上的位置 CGPoint uiPoint = [touch locationInView:touch.view]; // 转换为OpenGL坐标 CGPoint glPoint = [[CCDirector sharedDirector] convertToGL:uiPoint]; return glPoint; } // 由于ccTouchesBegan、ccTouchesMoved、ccTouchesEnded中的做法都是一样,所以抽成一个方法 - (void)dealTouches:(NSSet *)touches { // 计算触摸的位置 CGPoint point = [self locationInLayer:touches]; // 根据标记获取精灵 CCSprite *lufy = (CCSprite *)[self getChildByTag:kLufyTag]; // 设置精灵的位置 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]; }
图层的触摸输入暂时讲到这里,其他高级的用法在后面会提及
2.接收加速计输入
CCLayer默认情况是不接收加速计输入的,需要显示地设置isAccelerometerEnabled为YES
[java] viewplaincopyself.isAccelerometerEnabled = YES; 设置isAccelerometerEnabled为YES后,就会调用图层相应的方法来处理加速计输入:这是在UIAccelerometerDelegate协议中定义的方法
[java] viewplaincopy- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { // typedef double UIAccelerationValue; UIAccelerationValue x = acceleration.x; UIAccelerationValue y = acceleration.y; UIAccelerationValue z = acceleration.z; // x,y,z代表三维中任意方向的加速度 }
CCLayerColor
有时候,我们想给整个图层设置一种背景颜色,那么就需要用到CCLayerColor了,CCLayerColor是CCLayer的子类
[java] view
plaincopy
// 红色:#ffff0000
ccColor4B color = ccc4(255, 0, 255);
// 初始化一个颜色图层
CCLayerColor *layerColor = [CCLayerColor layerWithColor:color];
// 添加到场景中
[scene addChild:layerColor];
效果图:
CCLayerGradient
CCLayerGradient是CCLayerColor的子类,可以给图层设置渐变色
[java] view
plaincopy
// 红色:#ffff0000
ccColor4B red = ccc4(255, 255);
// 蓝色:#ff0000ff
ccColor4B blue = ccc4(0, 255, 255);
// 初始化一个渐变图层,从红色渐变到蓝色
CCLayerGradient *layerGradient = [CCLayerGradient layerWithColor:red fadingTo:blue];
// 添加到场景中
[scene addChild:layerGradient];
效果图:
CCLayerMultiplex
CCLayerMultiplex继承自CCLayer,称为"多重图层"。它可以包含多个CCLayer对象,但在任意时刻只可以有一个CCLayer处于活动状态,用switchTo:和switchToAndReleaseMe:方法可以让某个图层处于活动状态,区别在于switchToAndReleaseMe:方法会先释放当前处于活动状态的图层,再让参数中要求的图层处于活动状态
[java] view
plaincopy
// 创建2个图层
CCLayer *layer1 = [CCLayer node];
CCLayer *layer2 = [CCLayer node];
// 创建一个多重图层,包含了layer1和layer2
CCLayerMultiplex *plex = [CCLayerMultiplex layerWithLayers:layer1, layer2, nil];
// 让layer1处于活动状态(layer2还在内存中)
[plex switchTo:0];
// 让layer2处于活动状态(layer1还在内存中)
[plex switchTo:1];
// 释放当前处于活动状态的layer2(layer2从内存中移除),然后让layer1处于活动状态
[plex switchToAndReleaseMe:0];
图层之间的切换是没有过渡效果的
转载地址:点击打开链接