0

我正在尝试在代码中加载几个大模型并显示场景。通常,加载和在屏幕上显示场景需要很长时间,因为它需要从 pod 模型中提取大量资源。所以,我想在主线程中填充第一个场景,并将其他场景留在单独的线程中。但是,当我将部分代码移动到单独的线程中时,它会崩溃。这是我的示例代码:

    -(void) loadFirstScene
{
CC3PODResourceNode* podRezNode = [CC3PODResourceNode nodeWithName: @"FirstModel"];
podRezNode.resource = [IntroducingPODResource resourceFromFile: @"FirstModel.pod"];
podRezNode.shouldCullBackFaces = NO;
podRezNode.location = cc3v(0.0, -10.0, 0.2);
podRezNode.isTouchEnabled = YES;
[self addChild: podRezNode];

[NSThread detachNewThreadSelector:@selector(loadScenesInThread) toTarget:self
withObject:nil];
}

// Crashing if i add the below function in separate thread or background thread
-(void) loadScenesInThread
{
CC3PODResourceNode* podRezNode = [CC3PODResourceNode nodeWithName: @"SecondModel"];
podRezNode.resource = [IntroducingPODResource resourceFromFile: @"SecondModel.pod"];

podRezNode.shouldCullBackFaces = NO;
podRezNode.location = cc3v(0.0, -10.0, -5.0);
podRezNode.isTouchEnabled = YES;
[self addChild: podRezNode];

podRezNode = [CC3PODResourceNode nodeWithName: @"ThirdModel"];
podRezNode.resource = [IntroducingPODResource resourceFromFile: @"ThirdModel.pod"];

podRezNode.shouldCullBackFaces = NO;
podRezNode.location = cc3v(0.0, -10.0, -5.0);
podRezNode.isTouchEnabled = YES;
[self addChild: podRezNode];

// .. do more
}

有人可以指导我如何处理这种情况吗?

4

1 回答 1

0

您不能在与创建 OpenGL 上下文的线程不同的线程中创建或访问 OpenGL 资源。您唯一的选择是使用 cocos2d 提供的任何“异步”方法,或者加载纹理的示例。

于 2013-04-15T18:35:01.563 回答