cocos2D(六)----CCLayer,cocos2d----cclayer
一個遊戲中可以有很多個情境,每個情境裡面又可能包含有多個圖層,這裡的圖層一般就是CCLayer對象。CCLayer本身幾乎沒什麼功能,對比CCNode,CCLayer可用於接收觸摸和加速計輸入。其實,cocos2d對圖層並沒有嚴格的要求,圖層不一定要使用CCLayer類,它也可以是一個簡單的CCNode,為什麼呢?我們建立一個圖層不就是為了能夠容納更多的子節點麼,CCNode也可以添加子節點啊。所以,如果你的圖層不需要接收觸摸和加速計輸入,就盡量使用CCNode表示圖層,CCLayer因為能夠接收觸摸和加速計輸入會增加不必要的開銷。移動、縮放、旋轉整個圖層,圖層上的所有節點也會跟著一起移動、縮放、旋轉。
常用設定
1.接收觸摸輸入
CCLayer預設情況是不接收觸摸輸入的,需要顯示地設定isTouchEnabled為YES
[java] view plaincopy
- self.isTouchEnabled = YES;
設定isTouchEnabled為YES後,就會調用圖層相應的方法來處理觸摸輸入:
這些都是在CCStandardTouchDelegate協議中定義的方法
1> 當單指接觸到螢幕時
[java] view plaincopy
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
2> 當手指在螢幕上移動時
[java] view plaincopy
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
3> 當單指離開螢幕時
[java] view plaincopy
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
4> 當觸摸被取消時
[java] view plaincopy
- - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
很少會發生觸摸被取消的情況,所以大多數情況下可忽略,或用ccTouchesEnded代替,因為ccTouchesCancelled和ccTouchesEnded類似
大部分情況下,我們需要知道觸摸發生在什麼位置。這裡的觸摸事件是由UIKit架構接收的,因此需要把觸摸位置轉換為OpenGL座標。
比如在手指移動過程中:
[java] view plaincopy
- - (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] view plaincopy
- // 圖層的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] view plaincopy
- // 計算觸摸在圖層中的位置(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] view plaincopy
- self.isAccelerometerEnabled = YES;
設定isAccelerometerEnabled為YES後,就會調用圖層相應的方法來處理加速計輸入:
這是在UIAccelerometerDelegate協議中定義的方法
[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,z代表三維中任意方向的加速度
- }
CCLayerColor
有時候,我們想給整個圖層設定一種背景顏色,那麼就需要用到CCLayerColor了,CCLayerColor是CCLayer的子類
[java] view plaincopy
- // 紅色:#ffff0000
- ccColor4B color = ccc4(255, 0, 0, 255);
- // 初始化一個顏色圖層
- CCLayerColor *layerColor = [CCLayerColor layerWithColor:color];
- // 添加到情境中
- [scene addChild:layerColor];
:
CCLayerGradient
CCLayerGradient是CCLayerColor的子類,可以給圖層設定漸層色
[java] view plaincopy
- // 紅色:#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:方法會先釋放當前處於活動狀態的圖層,再讓參數中要求的圖層處於活動狀態
[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];
圖層之間的切換是沒有過渡效果的
原文地址:http://blog.csdn.net/q199109106q/article/details/8601533
感謝作者~!
COCOS2d中建立一個CCLayer層為何顯示不出來
請問你add了嗎?
怎從sdk(UIController)跳轉到cocos2d(CClayer)
這個還這麼沒這麼弄過,你在工程中直接引入2d的庫試試看