【问题标题】:Slow preformance during replaceScenereplaceScene 期间性能缓慢
【发布时间】:2012-11-13 07:15:11
【问题描述】:

每当代码到达“replaceScene”时,我都会遇到性能问题。它只发生在播放场景中。所以游戏结束后,我会显示一个分数,然后是 CCDirector 执行 replaceScene 以返回主菜单的时候了。

等待大约 20 秒后,它终于显示主菜单。但不知何故这是不对的,玩家会觉得游戏突然挂了。然后我试着放了一些像预加载器这样的动画,它发生了同样的情况,预加载器图片做了一段时间的动画然后突然停止,我认为由于replaceScene触发了同样的问题,虽然它仍然会显示主菜单场景。小心提供一些技巧,如何加快所有不再需要的对象的释放。

希望从这里的专家那里得到解决方案。谢谢。

这是我的代码:

............
//button at the score pop up sprite

CCMenuItem *btContinue = [CCMenuItemImage itemFromNormalImage:BTCONTINUE 
                                                selectedImage:BTCONTINUE_ON 
                                                       target:self 
                                                     selector:@selector(goLoader)];
btContinue.anchorPoint = ccp(0,0);
btContinue.position = ccp(340, 40);

CCMenu *menu = [CCMenu menuWithItems:btContinue, nil];
menu.position = CGPointZero;

[self addChild:menu z:ZPOPUP_CONTENT];

//prepare the loader, but set visible to NO first
CCSprite *loaderBg = [CCSprite spriteWithFile:LOADER_FINISH];
loaderBg.anchorPoint = ccp(0,0);
loaderBg.position = ccp(0,0);
loaderBg.visible = NO;
[self addChild:loaderBg z:ZLOADER_BG tag:TAG_LOADER_BG];
NSLog(@"prepare loader finish");

//animate loader
CCSprite *loaderPic = [[CCSprite alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                                                          spriteFrameByName:LOADER]];    
loaderPic.anchorPoint = ccp(0.5,0.5);
loaderPic.position = ccp(200,35);
loaderPic.visible = NO;
[self addChild:loaderPic z:ZLOADER_PIC tag:TAG_LOADER_PIC];

[loaderPic runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:0.05f angle:10.0f]]];
}

-(void)goLoader {
NSLog(@"goMainMenuScene");
CCSprite *tmpBg = (CCSprite *) [self getChildByTag:TAG_LOADER_BG];
if (tmpBg != nil)
    tmpBg.visible = YES;

CCSprite *tmpPic = (CCSprite *) [self getChildByTag:TAG_LOADER_PIC];
if (tmpPic != nil)
    tmpPic.visible = YES;

double time = 2.0;
id delay = [CCDelayTime actionWithDuration: time];
id proceed = [CCCallFunc actionWithTarget:self selector:@selector(goMainMenuScene)];
id seq = [CCSequence actions: delay, proceed, nil];
[self runAction:seq];
}

-(void)goMainMenuScene {
[[GameManager sharedGameManager] runSceneWithID:SCENE_MAIN_MENU];
}

【问题讨论】:

  • 您正在主线程上进行一些繁重的计算。除非您确定导致所有这些延迟的原因,然后移动该代码并在后台线程上执行它,否则添加加载动画将不起作用。找出问题的最佳方法是使用 Xcode Instruments 工具中的 time profiler
  • 您使用了多少内存?我假设您正在将大量资源加载到主线程的内存(声音,艺术)中。
  • 获取我在那个场景中使用了多少内存的信息的最佳方法是什么?
  • @Mazyod:thx,我是乐器新手,以后肯定会尝试,但是其他人如何制作预加载器图片呢?
  • @Wes:他们按照我的解释去做。他们将需要大量时间执行的代码移至后台线程。 [self performSelectorInBackground:@selector(loadAssets) withObject:nil];.

标签: iphone cocos2d-iphone


【解决方案1】:

您的问题很可能是新场景,以及新场景及其子节点的 init 方法中发生的任何事情。加载资源文件可能需要很长时间。如果您推迟到 onEnter 执行此操作,您可能会看到更好的结果。但是20秒,已经很多了。检查你在做什么需要这么长时间。我敢打赌它正在加载大量资源,或者以极其低效的方式加载它们。众所周知,JPG 文件加载非常缓慢,如果您使用 JPG 将它们转换为 PNG。

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 2010-10-14
    • 2015-01-27
    • 2021-06-04
    • 2016-11-09
    • 2021-06-21
    • 2012-05-11
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多