0

私のゲームシーンでは、CCParallaxNode で 5 つの背景画像 (スプライト) を使用しています。このすべてのレイヤーを永久に移動するには、各スプライトを 2 回使用する必要があります。このスプライトをキャッシュしてメモリを節約するにはどうすればよいですか?

CCSprite * for _level_1およびCCSprite * for _level_2 cuzからのエスケープが必要です。同じ画像が 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];

メソッド、フォンを移動し、背景レイヤーの最後を達成することを確認します

-(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];
        }
    }
}
4

1 に答える 1

1

テクスチャをテクスチャキャッシュに一度だけ追加する必要があります。CCSpriteインスタンスはテクスチャをコピーしません。テクスチャとフレームパラメータ(原点とサイズ)へのポインタを格納するだけで、テクスチャのどの部分を使用するかを知ることができます(たとえば、多数のスプライトがパックされた大きなアトラスがある場合)。

実際には、テクスチャをテクスチャキャッシュに直接追加しない場合があります。CCSpriteインスタンスのinitメソッドのテクスチャキャッシュに追加されます。

于 2012-04-18T15:08:22.010 に答える