0

cocos2d ゲームで問題が発生しました。ゲームのレベル レイヤーで、背景用に 3 ~ 5 個の大きなスプライト (それぞれ 1920*640 ~ 100kb) ~ 各レベルで 400 ~ 500kb を使用しています。メニューとさまざまなゲームのレベルを 4 ~ 5 回切り替えると、ゲームがメインの iPhone メニューにクラッシュします。

[[CCDirector sharedDirector] replaceScene:transition];

この大きなスプライトがなくても、すべて完璧に機能します!

-(id) init
{
 ....   
    if( (self=[super init])) {
_spriteBGLevel1 = [CCSprite spriteWithFile: [NSString stringWithFormat:@"level_fon_%i_1.png", _currentLevel]]; 
_spriteBGLevel1.anchorPoint = ccp(0, 0);
_spriteBGLevel1.position = CGPointMake(0.0, 0.0); 
[self addChild:_spriteBGLevel1 z:-5];
_spriteBGLevel2 = [CCSprite spriteWithFile: [NSString stringWithFormat:@"level_fon_%i_2.png", _currentLevel]]; 
_spriteBGLevel2.anchorPoint = ccp(0, 0);
_spriteBGLevel2.position = CGPointMake(0.0, 0.0); 
[self addChild:_spriteBGLevel2 z:-5];
_spriteBGLevel3 = [CCSprite spriteWithFile: [NSString stringWithFormat:@"level_fon_%i_3.png", _currentLevel]]; 
_spriteBGLevel3.anchorPoint = ccp(0, 0);
_spriteBGLevel3.position = CGPointMake(0.0, 0.0); 
[self addChild:_spriteBGLevel3 z:-5];        

....


- (void) dealloc
{
....
    _spriteBGLevel1=nil;
    _spriteBGLevel2=nil;
    _spriteBGLevel3=nil;
    [super dealloc];
}

背景.m

#import "Background.h"
#import "GameConfig.h"
#import "XMLReader.h"
#import "CCParallaxNode-Extras.h"
#import "SettingsManager.h"


@implementation Background

@synthesize backgrounds=_backgrounds;
@synthesize backgroundNode=_backgroundNode;

+(id) scene
{
    CCScene *scene = [CCScene node];

    Background *layer = [Background node];

    [scene addChild: layer];

    return scene;
}

-(id) init
{
    if ((self = [super init]))
    {

        CGSize screenSize = [[CCDirector sharedDirector] winSize];

        int currentLevel = [[SettingsManager sharedSettingsManager] getCurrentLevel];

        _backgroundNode = [CCParallaxNode node];
        _backgroundNode.anchorPoint = CGPointMake(0, 0);
        _backgroundNode.position = CGPointMake(0, 0);
        [self addChild:_backgroundNode z:-1];

        NSData *xmlData = [NSData dataWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"gameScene" ofType:@"xml"]];

        NSError *error = nil;

        NSDictionary *dictionary = [XMLReader dictionaryForXMLData:xmlData error:&error];
        NSDictionary *levelsDict = [dictionary valueForKeyPath:@"levels.level"];
        NSDictionary *levelDict;

        _backgrounds = [[[NSMutableArray alloc] init] retain];

//        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: [NSString stringWithFormat:@"level_fon_%i.plist", currentLevel]];


        for (levelDict in levelsDict)
        {

            int idLevel = [[levelDict valueForKeyPath:@"id"] intValue];
            if(idLevel==currentLevel)
            {
                NSDictionary *fonsDict = [levelDict valueForKeyPath:@"background.fon"];

                NSDictionary *fonDict;
                for (fonDict in fonsDict){

                    NSString *name=[fonDict valueForKeyPath:@"name"];
                    int zIndex=[[fonDict valueForKeyPath:@"z"] intValue];
                    float ratio=[[fonDict valueForKeyPath:@"ratio"] floatValue];
                    float offsetx=[[fonDict valueForKeyPath:@"offsetx"] floatValue];
                    float offsety=[[fonDict valueForKeyPath:@"offsety"] floatValue];
                    if(zIndex<0)
                    {    
                    //CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:@"%@", name]];

                    //CCSprite *fon_level_1 = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"%@", name]];
                    //CCSprite *fon_level_2 = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"%@", name]];
                    //CCSprite *fon_level_1 = [CCSprite spriteWithTexture:tex];
                    //CCSprite *fon_level_2 = [CCSprite spriteWithTexture:tex];

                    fon_level_1 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@", name]];
                    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_level_1=nil;
                        fon_level_2=nil;
                    }    
                }
                break;
            }    
        }

        NSLog(@"count: %d",_backgrounds.count);
        [self scheduleUpdate];
    }
    return self;
}
- (void)scheduleUpdate
{
    [self schedule:@selector(updateBackgroud:)];
}
-(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) dealloc
{
     _backgroundNode = nil;
    // _backgrounds = nil;
    [_backgrounds removeAllChildren]; 
    [_backgrounds release];
    [super dealloc];
}
@end
4

1 に答える 1

1

さて、これが私がそれについて行った方法です:

  • POT テクスチャ サイズにできるだけ近づけます。1920x640 は、2048x2048 と同じくらいのメモリを消費します。おそらく、アーティストに 3 ~ 5 個のスプライトを 1 つの 2048x2848 png に結合させ、plist で切り抜く (アニメーションのように) ようにさせることができます。
  • 可能な限り、テクスチャを「ジャスト イン タイム」にロードします。つまり、テクスチャを表示する必要がある前にロードします。ロード時間が問題になった場合 (不適切なゲーム環境でラグが認識される場合)、テクスチャを .pvr 形式 (より高速なロード) に変換し、後で .pvr.gz (より高速なロード時間、より小さなアプリのダウンロード サイズ) に変換します。PVR はいくつかのアーティファクトを引き起こすため、コミットする前にプロジェクトのグラフィック担当者に確認してください。
  • レベル トランジション (ある種のメニューまたはカット シーンのいずれか) から出て、テクスチャによって使用されるメモリをクリーンアップします。これは、ゲーム ロジックがメイン シーンに戻る必要がある場合、遷移中にジャストインタイムでテクスチャをリロードすることを意味します。
  • 最後に、これらの大きなテクスチャをロードする前に深度設定を変更します (以下のコード スニペット)。RGBA444 に設定すると、メモリ要件の 75% を節約できますが、特に彩度の高い画像の場合、グラフィック品質が低下します。繰り返しますが、そこに行く必要がある場合は、グラフィック担当者に画像をこの深さまで最適化してもらい、デバイスでの結果に満足してもらいます。

掃除:

NSLog(@"GESprite<removeUnusedSprites> : before purging all caches");
[[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCDirector sharedDirector] purgeCachedData];
NSLog(@"GESprite<removeUnusedSprites> : after purging all caches");
[[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];

深さ (CCSprite 派生クラス、いくつかのユーティリティ メソッド) で、いつでもどこでもこれを行うことができます。

+(void) mediumPixelFormat{
    [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444];
}

+(void) highPixelFormat{
    [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
}
于 2012-07-22T13:26:00.403 に答える