Caching 缓存精灵以保存内存

Caching 缓存精灵以保存内存,caching,cocos2d-iphone,sprite,Caching,Cocos2d Iphone,Sprite,在我的游戏场景中,我在CCParallaxNode中使用了5个背景图像(精灵),我必须使用每个精灵两次才能永久移动所有这些层。如何缓存此精灵以节省内存 我只想从CCSprite*中逃脱一级和CCSprite*二级因为它两次都是同一个图像 _backgroundNode = [CCParallaxNode node]; _backgroundNode.anchorPoint = CGPointMake(0, 0); _backgroundNode.position = CGPointMake(0,

在我的游戏场景中,我在CCParallaxNode中使用了5个背景图像(精灵),我必须使用每个精灵两次才能永久移动所有这些层。如何缓存此精灵以节省内存

我只想从CCSprite*中逃脱一级CCSprite*二级因为它两次都是同一个图像

_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];
方法,其中移动FON并检查背景层的结束

-(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];
        }
    }
}
-(void)updatebackground:(ccTime)delta
{
CGPoint BackgroundScrollLevel=ccp(-1000,0);
_backgroundNode.position=ccpAdd(_backgroundNode.position,ccpMult(backgroundScrollVel,delta));
用于(CCSprite*背景在_背景中){
if([[u backgroundNode convertToWorldSpace:background.position].x+background.contentSize.width/10)<-(background.contentSize.width)){
[\u backgroundNode incrementOffset:ccp(background.contentSize.width*2,0)forChild:background];
}
}
}

您应该只将纹理添加到纹理缓存一次,CCSprite实例不会复制纹理。它只存储一个指向纹理和帧参数(原点和大小)的指针,以了解要使用的纹理部分(例如,如果有一个包含大量精灵的大型图集)


实际上,您可能不希望直接将纹理添加到纹理缓存。它将被添加到CCSprite实例init方法中的纹理缓存中。

示例?O_O CCSprite*sprite=[CCSprite spriteWithFile:filePath];在这行代码之后,您的纹理将加载到纹理缓存中,下一个sprite实例将在纹理缓存中找到此纹理,并使用它而不是创建纹理copyCCTexture2D*tex=[[CCTextureCache sharedTextureCache]addImage:[NSString stringWithFormat:@“%@”,name]];CCSprite*fon_level_1=[CCSprite-spriteWithTexture:tex];CCSprite*fon_level_2=[CCSprite-spriteWithTexture:tex];或者它是额外的,只需要两次CCSprite*fon_level_1=[CCSprite spriteWithFile:[NSString stringWithFormat:@“%@”,name]];?您可以将sprite与文件一起使用。有了记忆,一切都会好起来的。我更喜欢用纹理构造函数避免spriteWithTexture。如果您将精灵打包在一个大图集中(例如,2048x2048像素),您将获得具有整个巨大纹理的精灵