一個遊戲中可以有很多個場景,每個場景裏面又可能包含有多個圖層,這裏的圖層一般就是CCLayer對象。CCLayer本身幾乎沒什麼功能,對比CCNode,CCLayer可用於接收觸摸和加速計輸入。其實,cocos2d對圖層並沒有嚴格的要求,圖層不一定要使用CCLayer類,它也可以是一個簡單的CCNode,爲什麼呢?我們新建一個圖層不就是爲了能夠容納更多的子節點麼,CCNode也可以添加子節點啊。所以,如果你的圖層不需要接收觸摸和加速計輸入,就儘量使用CCNode表示圖層,CCLayer因爲能夠接收觸摸和加速計輸入會增加不必要的開銷。移動、縮放、旋轉整個圖層,圖層上的所有節點也會跟着一起移動、縮放、旋轉。
常用設置
1.接收觸摸輸入
CCLayer默認情況是不接收觸摸輸入的,需要顯示地設置isTouchEnabled爲YES
- self.isTouchEnabled = YES;
這些都是在CCStandardTouchDelegate協議中定義的方法
1> 當單指接觸到屏幕時
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
2> 當手指在屏幕上移動時
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
3> 當單指離開屏幕時
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
4> 當觸摸被取消時
- - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
大部分情況下,我們需要知道觸摸發生在什麼位置。這裏的觸摸事件是由UIKit框架接收的,因此需要把觸摸位置轉換爲OpenGL座標。
比如在手指移動過程中:
- - (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];
- }
下面利用一個小例子來綜合使用上述的方法,假設圖層上有個精靈,我手指觸摸到哪,這個精靈的位置就在哪
首先在圖層初始化的時候添加精靈
- // 圖層的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;
- }
接下來是在圖層中接收觸摸輸入
- // 計算觸摸在圖層中的位置(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
- self.isAccelerometerEnabled = YES;
這是在UIAccelerometerDelegate協議中定義的方法
- - (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的子類
- // 紅色:#ffff0000
- ccColor4B color = ccc4(255, 0, 0, 255);
- // 初始化一個顏色圖層
- CCLayerColor *layerColor = [CCLayerColor layerWithColor:color];
- // 添加到場景中
- [scene addChild:layerColor];
CCLayerGradient
CCLayerGradient是CCLayerColor的子類,可以給圖層設置漸變色
- // 紅色:#ffff0000
- ccColor4B red = ccc4(255, 0, 0, 255);
- // 藍色:#ff0000ff
- ccColor4B blue = ccc4(0, 0, 255, 255);
- // 初始化一個漸變圖層,從紅色漸變到藍色
- CCLayerGradient *layerGradient = [CCLayerGradient layerWithColor:red fadingTo:blue];
- // 添加到場景中
- [scene addChild:layerGradient];
CCLayerMultiplex
CCLayerMultiplex繼承自CCLayer,稱爲"多重圖層"。它可以包含多個CCLayer對象,但在任意時刻只可以有一個CCLayer處於活動狀態,用switchTo:和switchToAndReleaseMe:方法可以讓某個圖層處於活動狀態,區別在於switchToAndReleaseMe:方法會先釋放當前處於活動狀態的圖層,再讓參數中要求的圖層處於活動狀態
- // 創建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];
圖層之間的切換是沒有過渡效果的
原文地址:http://blog.csdn.net/q199109106q/article/details/8601533
感謝作者~!