0
我在CCParallaxNode中使用了5個背景圖像(精靈),我必須使用每個精靈兩次來永久移動所有這些圖層。我如何緩存這個精靈來保存內存?緩存精靈在我的遊戲場景中保存內存
我只是想從CCSprite *逃避_level_1和CCSprite *爲_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];
}
}
}
你能寫出例子嗎? – 2012-04-18 15:21:39
例子? O_o CCSprite * sprite = [CCSprite spriteWithFile:filePath]; 在這行代碼之後你的紋理將被加載到紋理緩存中,下一個sprite實例實例將在紋理緩存中找到這個紋理,並使用它來代替創建紋理拷貝 – Morion 2012-04-18 15:31:20
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