Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)
问题描述
cocos2d场景在上一个之后如何重新启动并清理内存?
[director replaceScene:s]
正在显示粉色屏幕.无法让它工作.
[director replaceScene:s]
is displaying pink screen. Can't get it to work.
我所做的解决方法如下,但是在关闭场景后,应用程序运行速度非常慢并且对触摸的反应很慢.MainViewController.m:
The work-around I made is below, however after closing scene this way app runs very slow and slowly reacts to touch. MainViewController.m:
- (IBAction)startScene {
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
if ([director runningScene]) {
[director end];
works = NO;
}
if (!works) {
EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0,0, 768, 1024)];
[self.view addSubview:glview];
[director setOpenGLView:glview];
CCLayer *layer = [PlayLayer node];
CCScene *s = [CCScene node];
[s addChild: layer];
[director runWithScene:s];
}
}
在 PlayLayer.m 我从场景返回查看使用:
Inside PlayLayer.m I go back from scene to view using:
CCDirector *d = [CCDirector sharedDirector];
EAGLView *v = [d openGLView];
[v removeFromSuperview];
应用程序正在显示图像(1280x960x32,滚动视图内的图像视图),当用户按下按钮时,他可以启动 cocos2d 场景.然后,用户可以从场景返回查看 (.xib) 并继续浏览图像,直到找到新的开始场景.
Application is displaying images (1280x960x32, imageview inside scrollview) and when user presses the button he can start cocos2d scene. User can then leave from scene back to view (.xib) and continue browsing through images till he finds a new one to start scene with.
我相信这不是离开场景的好方法,但我尝试的所有其他方法都会导致应用程序崩溃,或者我第二次开始场景时总是出现这个粉红色的屏幕.怎么重启?
I believe this is not a good way to leave scene but everything else I tried is causing app to crash or I keep getting this pink screen 2nd time I start scene. How do I restart it ?
推荐答案
好的,找到了一个从内部关闭场景并且效果很好的解决方案.
Ok, found a solution that closes the scene from inside and works well.
在开始场景时,我在视图控制器中添加通知观察者:
While starting scene I add the notification observer in my viewcontroller:
(...)
[director runWithScene:s];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mySceneEnd:) name:@"scene_ended" object:nil];
连接到:
- (void) mySceneEnd:(NSNotification *)notif {
if ([director runningScene])
[director end];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
然后在场景结束方法中,我将通知发送到我的视图控制器:
Then in the scene ending method I'm sending the notification to my viewcontroller:
EAGLView *v = [[CCDirector sharedDirector] openGLView];
[v removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@"scene_ended"
object:nil userInfo:nil];
效果非常好,关闭场景后应用程序运行速度很快,但这是一个老问题/答案,现在肯定有更好的方法.
Works really nice, app runs fast after closing scene, but it's an old question/answer and there is surely a better way to do it now.
这篇关于正确的 cocos2d 场景重启?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:正确的 cocos2d 场景重启?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01