2012-04-18 86 views
0

我在CCParallaxNode中使用了5個背景圖像(精靈),我必須使用每個精靈兩次來永久移動所有這些圖層。我如何緩存這個精靈來保存內存?緩存精靈在我的遊戲場景中保存內存

我只是想從CCSprite *逃避_level_1CCSprite *爲_level_2 cuz它相同的圖像兩次。

_backgroundNode = [CCParallaxNode node]; 
_backgroundNode.anchorPoint = CGPointMake(0, 0); 
_backgroundNode.position = CGPointMake(0, 0); 
[self addChild:_backgroundNode z:-1]; 

.....環路

  CCSprite *fon_level_1 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@", name]]; 
      CCSprite *fon_level_2 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@", name]]; 
      fon_level_1.anchorPoint = CGPointMake(0, 0); 
      fon_level_1.position = CGPointMake(0, 0); 
      fon_level_2.anchorPoint = CGPointMake(0, 0); 
      fon_level_2.position = CGPointMake(0, 0); 
      [_backgroundNode addChild:fon_level_1 z:zIndex parallaxRatio:ccp(ratio, ratio) positionOffset:ccp(offsetx, offsety*screenSize.height)]; 
      [_backgroundNode addChild:fon_level_2 z:zIndex parallaxRatio:ccp(ratio, ratio) positionOffset:ccp(fon_level_1.contentSize.width, offsety*screenSize.height)]; 
      [_backgrounds addObject:fon_level_1]; 
      [_backgrounds addObject:fon_level_2]; 

方法,其中移動FONS和檢查實現背景層端

-(void) updateBackgroud:(ccTime)delta 
{ 
    CGPoint backgroundScrollVel = ccp(-1000, 0); 

    _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, delta)); 

    for (CCSprite *background in _backgrounds) { 
     if (([_backgroundNode convertToWorldSpace:background.position].x+background.contentSize.width/10) < -(background.contentSize.width)) { 
      [_backgroundNode incrementOffset:ccp(background.contentSize.width*2,0) forChild:background]; 
     } 
    } 
} 

回答

1

你應該添加紋理緩存到紋理緩存一次,CCSprite實例將不會複製紋理。它只是存儲一個指向紋理和框架參數(起點和大小)的指針,以知道要使用哪個紋理部分(例如,如果你有一個包含大量精靈的大圖集)。

實際上,您可能不會直接將紋理添加到紋理緩存中。它將被添加到CCSprite實例init方法中的紋理緩存中。

+0

你能寫出例子嗎? – 2012-04-18 15:21:39

+0

例子? O_o CCSprite * sprite = [CCSprite spriteWithFile:filePath]; 在這行代碼之後你的紋理將被加載到紋理緩存中,下一個sprite實例實例將在紋理緩存中找到這個紋理,並使用它來代替創建紋理拷貝 – Morion 2012-04-18 15:31:20

+0

CCTexture2D * tex = [[CCTextureCache sharedTextureCache] addImage:[ NSString stringWithFormat:@「%@」,name]]; CCSprite * fon_level_1 = [CCSprite spriteWithTexture:tex]; CCSprite * fon_level_2 = [CCSprite spriteWithTexture:tex]; – 2012-04-18 15:47:41