Cocos3d:在单独的线程或后台线程中加载场景时崩溃:

时间:2013-04-15 17:05:56

标签: cocos3d

我正在尝试在代码中加载几个大型模型并显示场景。通常,由于需要从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
}

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

1 个答案:

答案 0 :(得分:0)

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