【发布时间】:2012-04-18 23:08:22
【问题描述】:
在我的游戏场景中,我在 CCParallaxNode 中使用了 5 个背景图像(精灵),我必须使用每个精灵两次来永久移动所有这些图层。如何缓存这些精灵以节省内存?
我只想逃离 CCSprite *for _level_1 和 CCSprite *for _level_2 因为它两次是同一张图片。
_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];
}
}
}
【问题讨论】:
标签: caching cocos2d-iphone sprite