首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏后台崩溃3-5个大精灵(1920*640 ~100kb ),每个关卡400-500kb

游戏后台崩溃3-5个大精灵(1920*640 ~100kb ),每个关卡400-500kb
EN

Stack Overflow用户
提问于 2012-07-22 17:43:01
回答 1查看 437关注 0票数 0

我在我的cocos2d游戏中遇到了一个问题:在游戏的关卡层,我有3-5个大的精灵作为背景(1920*640 ~100kb ),每个关卡有400-500kb。当我在菜单和不同级别的游戏之间切换4-5次时,游戏会崩溃到主IPhone菜单

代码语言:javascript
复制
[[CCDirector sharedDirector] replaceScene:transition];

没有这个大的精灵,所有的工作都很完美!

代码语言:javascript
复制
-(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];
}

Background.m

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-22 21:26:00

好吧,我是这么做的:

  • 使其尽可能接近POT纹理大小。1920x640的内存成本相当于2048x2048。也许你可以让你的艺术家在一个2048x2848PNG上组合三到五个精灵,用plist裁剪(就像animation).
  • Whenever一样,在时间上加载纹理,即在它们被要求可见之前)。如果加载时间成为一个问题(在不适当的游戏环境中可察觉到延迟),请将纹理转换为.pvr格式(加载速度更快),然后转换为.pvr.gz (加载时间更快,应用程序下载大小更小)。PVR会导致一些伪像,所以在comiting.
  • Coming出任何级别转换(无论是到某种菜单还是到某个剪切场景)之前,请检查项目中的图形人员,清理纹理使用的内存。这意味着,如果游戏逻辑需要返回到主场景,在transition.
  • Last沟渠期间及时重新加载纹理,请在加载这些大型纹理之前更改深度设置(下面的代码片段)。设置为RGBA444将节省75%的内存需求,但您会影响一些图形质量,特别是对于高饱和度图像。再说一次,如果你需要去那里,让你的图形人员将图像优化到这个深度,这样他们就会对设备上的结果感到满意。

清理:

代码语言:javascript
复制
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派生类中,一些实用方法),您可以随时随地执行此操作:

代码语言:javascript
复制
+(void) mediumPixelFormat{
    [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444];
}

+(void) highPixelFormat{
    [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11599215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档