Reputation: 169
in my games scene i use 5 background images(sprites) in CCParallaxNode and I have to use each sprite twice to move all this layers permanently. How can I cache this sprites to save the memory?
I just want escape from CCSprite *for _level_1 and CCSprite *for _level_2 cuz it the same image twice.
_backgroundNode = [CCParallaxNode node];
_backgroundNode.anchorPoint = CGPointMake(0, 0);
_backgroundNode.position = CGPointMake(0, 0);
[self addChild:_backgroundNode z:-1];
.....
in loop
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];
method, where move the fons and check achieving the end of backgrounds layers
-(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];
}
}
}
Upvotes: 0
Views: 359
Reputation: 10860
you should add your texture to the texture cache only once, the CCSprite instance will not copy texture. It just stores a pointer to the texture and frame parameters(origin and size) to know, what part of texture to use(for example, if you have a big atlas with a lot of sprites packed in it).
actually, you may not to add your texture to the texture cache directly. it will be added to the texture cache in the CCSprite instance init method.
Upvotes: 1