接著上次的基礎知識,我們今天講解后面的知識點
1.CCscene 場景 ,這個類主要是說明和場景切換有關的類
- 創建、初始化一個場景
~~~
//1)創建一個 場景切換的類
//初始化或者創建一個一個場景
CCTransitionScene *transitionScene = [CCTransitionScene transitionWithDuration:4 scene:newScene];
[transitionScene initWithDuration:1 scene:newScene];
~~~
- 場景過渡效果結束
~~~
//2過渡效果結束的時候使用
[transitionScene finish];
~~~
-
~~~
//3部分過渡效果會使用該方法隱藏更外層的場景
[transitionScene hideOutShowIn];
//一個實例
~~~
- 這是關于一個場景的延遲出現。
~~~
///**creates a base transition with duration and incoming scene */
// +(id) transitionWithDuration:(ccTime) t scene:(CCScene*)s;
~~~
2。CCLayer
~~~
//2CCLayer類的使用
//1)作用 其他子節點的的容器和組織者
//2 接受觸摸事件
self.isTouchEnabled = YES;
//3 接受加速器事件
self.isAccelerometerEnabled = YES;
//同樣,我們層里需要加入一個特定的方法來接受加速計事件
//4CCLayerColor色彩層
//可以設置RGB顏色的層,試下 CCRGBAprotocol協議的CCLayer子類
//1)CCLayerColor的屬性 color,opacity透明度,blendFunc.混合模式
//2)CClayerColor類的方法。
ccColor4F c4f = ccc4f(100, 100, 100, 100);
ccColor4B c4b = ccc4BFromccc4F(c4f);
//
CCLayerColor *layerColor = [[CCLayerColor alloc]init];
//初始化方法
CCLayerColor *layerColor1 = [[CCLayerColor alloc]initWithColor:c4b];
//初始化一個帶長寬的色彩層
CCLayerColor *layerColor2 =[[CCLayerColor alloc]initWithColor:c4b width: 100 height:100];
[layerColor2 changeHeight:10];
[layerColor2 changeWidth:22];
[layerColor2 changeWidth:10 height:22];
//5 CCLayerGrdient 漸變色層
//是CClayerColor的子類,繼承了其所有的屬性,增加了漸變方向,插值模式
//初始化一個特定漸變效果的色彩層
CCLayerGradient *gradient = [[CCLayerGradient alloc]initWithColor:c4b fadingTo:c4b:ccp(0, -1)];
//6CCMenu菜單類 MenuItem 類實例組成各種按鈕
//CCmenuItem 子類如下:
// 1)CCmenuItemLabel 點擊具有放大效果 iCCLabelBMFont ii:CCLabelAtlas,CCLabelTTF
CCLabelBMFont *label1 = [CCLabelBMFont labelWithString:@"Seeting" fntFile:@"font1.fnt"];
CCMenuItemLabel *item1 = [CCMenuItemLabel itemWithLabel:label1 target:self selector:nil];
//2)CCMenuItemAtlasFont,CCmenuItemSprite,CCMenuItemImage,CCMenuItemToggle
//7CCTexture 紋理類
//1)紋理 Texture,2)紋理圖集 TextureAylas
//3紋理的三種方式
//iCCTextureCache 單例使用,用于加載和管理紋理
NSString *filename = @"hell.png";
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:filename];
//ii:CCTextureAtlas
//8.CCSprite
CCSprite *sprite = [CCSprite spriteWithFile:filename];
//1.)紋理集的索引
[sprite setAtlasIndex:1];//通常不建議修改
//2對渲染CCSprite 的CCSpriteBatchNode的弱引用
//[sprite setBatchNode:<#(CCSpriteBatchNode *)#>]
//3 blendFunc 遵循CCtextureProtocol協議
//4 color:RGB顏色 遵循CCRGBAProtocol協議
//5dirty:判斷精靈是否需要在紋理集中更新
//6 filpx:判斷精靈是否會沿水平方向旋轉
//7 filpy
//8 offsetPosition 以點值計量精靈位置的偏移
//9 opacticy 透明度
//等
//9。精靈的類方法
//很多都是初始化方法,不再贅述
//10 CCSpriteBatchNode 精靈表單需要 OPENGL ES 一次性處理所有的紋理圖,減少GPU的壓力
//精靈表單的三種方法
//1.初始化一個精靈表單,可以最多容納 29*1.33
CCSpriteBatchNode *sprites = [CCSpriteBatchNode batchNodeWithFile:filename];
CCSpriteBatchNode *sprites2 = [CCSpriteBatchNode batchNodeWithFile:filename capacity:12];
//CCSpriteBatchNode *sprite3 = [[CCSpriteBatchNode batchNodeWithTexture:[CCTexture2D alloc]];
//2。移除一個子精靈,由于操作速度很慢,不建議使用
// [sprites removeChild:one cleanup:YES];
~~~
3.重啟游戲的場景切換
~~~
- (void)onRestartGame
{
CCTransitionFade *tramsitionFade = [CCTransitionFade transitionWithDuration:3 scene:[HelloWorldLayer scene] withColor:ccWHITE];
//重新開游戲的時候使用白色作為過度場景。 當然,除了 fade,還有很多別的過度場景
//使用過度場景對象替代helloworld場景,作為中間過渡場景,也就是延遲helloWorldLyer場景的出現
[[CCDirector sharedDirector]replaceScene:tramsitionFade];
}
~~~
4.加速計的使用
~~~
#pragma mark 如何操作加速計
/*
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float deceleration = 0.4f;
float sensitivity = 6.0f;
float maxVelocity = 100;
_playerVelocity.x =_playerVelocity.x *deceleration + acceleration.x *sensitivity;
if (_playerVelocity.x > maxVelocity)
_playerVelocity.x = maxVelocity;
else if(_playerVelocity.x < -maxVelocity)
_playerVelocity.x = -maxVelocity;
}
*/
~~~