2012-04-18 91 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