gpt4 book ai didi

objective-c - 正确的cocos2d场景重启?

转载 作者:行者123 更新时间:2023-12-04 06:36:29 25 4
gpt4 key购买 nike

如何在上一个场景之后重新启动 cocos2d 场景并清理内存?
[director replaceScene:s]正在显示粉红色屏幕。无法让它工作。

我所做的解决方法如下,但是在关闭场景后,这种方式应用程序运行非常缓慢并且对触摸的 react 很慢。主 View Controller .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 中,我从场景返回查看使用:
CCDirector *d = [CCDirector sharedDirector];
EAGLView *v = [d openGLView];
[v removeFromSuperview];

应用程序正在显示图像(1280x960x32, ScrollView 内的 ImageView ),当用户按下按钮时,他可以启动 cocos2d 场景。然后用户可以离开场景返回查看(.xib)并继续浏览图像,直到他找到一个新的开始场景。

我相信这不是离开场景的好方法,但我尝试过的其他一切都会导致应用程序崩溃,或者我第二次开始场景时总是出现这个粉红色屏幕。我如何重新启动它?

最佳答案

我终于可以在更换场景时摆脱粉红色屏幕。这就是我的做法。添加场景时,我检查它是第一次运行还是后续运行。

MyTestAppDelegate * appDelegate = (MyTestAppDelegate *)[[UIApplication sharedApplication] delegate];
EAGLView *glView;
//check if scene is running first time then create OpenGLView, add to director and run the scene
if ([[CCDirector sharedDirector] runningScene] == NULL) {
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];

CCDirector *director = [CCDirector sharedDirector];
glView = [EAGLView viewWithFrame:[appDelegate.window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
[appDelegate.window addSubview:glView];

[[CCDirector sharedDirector] runWithScene: [MyScene node]];
}
//if scene is to be reloaded, add back the openGLView which was removed when removing the scene
else {
[appDelegate.window addSubview:[[CCDirector sharedDirector] openGLView]];
[[CCDirector sharedDirector] replaceScene:[MyScene node]];
}

删除当前正在运行的场景:
[[CCDirector sharedDirector] replaceScene:[MySceneTwoLayer scene]];
[[CCDirector sharedDirector].openGLView removeFromSuperview];

添加 MySceneTwoLayer 是可选的。这是一个空旷的场景。我正在使用它来确保正确删除以前的场景。我检查了一下,正确调用了先前场景的 dealloc 。

我希望它有帮助。

关于objective-c - 正确的cocos2d场景重启?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4816051/

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