---
title: "sdk3.2升级到4.0,支持多线程cocos2d遇到问题的解决办法"
author: "wangzy_boy"
date: "2011-02-15T17:29:08Z"
tags: ["多线程"]
categories: ["移动开发"]
slug: "sdk3-2-4-0-cocos2d"
draft: false
lastmod: "2011-02-15T17:30:51Z"
id: 907104
url: "https://wangzy-boy.iteye.com/blog/907104"
---

```
- (void) swapBuffers
{
	// IMPORTANT:
	// - preconditions
	//	-> _context MUST be the OpenGL context
	//	-> _renderBuffer must be the the RENDER BUFFER

#ifdef __IPHONE_4_0
	if( _depthFormat && _discardFramebufferSupported ) {
		GLenum attachments[] = { GL_DEPTH_ATTACHMENT_OES };
		glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 1, attachments);
	}
#endif // __IPHONE_4_0
	
	if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES])
		CCLOG(@"cocos2d: Failed to swap renderbuffer in %s\n", __FUNCTION__);

#if COCOS2D_DEBUG
	CHECK_GL_ERROR();
#endif	
}
```

 

sdk升级到4.0后,cocos2d的上面的函数的

```
if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES])
```

这句在按home推出时会报错,导致不可以实现多线程,解决办法是修改delegate的函数如下

```
- (void)applicationWillResignActive:(UIApplication *)application {
        [[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
        [[CCDirector sharedDirector] stopAnimation]; 
	[[CCDirector sharedDirector] resume];
	[[CCDirector sharedDirector] startAnimation];
}
```

 

并增加两个函数

```
- (void)applicationWillEnterForeground:(UIApplication *)application
{
	[[CCDirector sharedDirector] startAnimation];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
        [[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] pause];
}
```

 这样修改完成后,就不会出错了,按home键程序处于刮起挂起状态,再切会来是,程序保持在推出之前的状态

iphone的多线程虽说技术上实现的不是很完美,但效果确实很不错,希望下一个版本的多线程能做的更好,期待阿...