gpt4 book ai didi

ios - 游戏崩溃 背景有 3-5 个大 Sprite (每个 1920*640 ~100kb)~ 每个关卡 400-500kb

转载 作者:行者123 更新时间:2023-11-29 04:32:05 26 4
gpt4 key购买 nike

我的 cocos2d 游戏遇到了一个问题:在游戏的关卡层中,我有 3-5 个大 Sprite 作为背景(每个 1920*640 ~100kb)~每个关卡 400-500kb。当我在菜单和各种游戏关卡之间切换 4-5 次时,游戏崩溃到 iPhone 主菜单<​​/p>

[[CCDirector sharedDirector] replaceScene:transition];

没有这个大 Sprite ,一切都很完美!

-(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

最佳答案

嗯,这就是我的做法:

  • 尽可能接近 POT 纹理尺寸。 1920x640 占用的内存与 2048x2048 占用的内存一样多。也许你可以让你的艺术家将三到五个 Sprite 组合在一个 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];
}

关于ios - 游戏崩溃 背景有 3-5 个大 Sprite (每个 1920*640 ~100kb)~ 每个关卡 400-500kb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com