【发布时间】: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