how to write this code in cocos2d-x

how to write this code in cocos2d-x
0.0 0


how to write this code in cocos2d-x

[self performSelectorInBackground:@selector(makeNextTranLayer) withObject:nil];


Hi, you can use pthead to create an sub-thread. Make sure that don’t use autorelease operation in your sub-thread.Otherwise, it’ll cause some problems because autorelease is not thread-safe.


like this?

pthread_t tid1;
pthread_create(&tid1, NULL, &loadGame, this);

static void * loadGame(void ptr)
pool = [[NSAutoreleasePool alloc] init];


//[pool release];
return NULL;

the function loadGame must be define static?


You shouldn’t invoke CCSpriteFrameCache::addSpriteFramesWithFile in a sub-thread.

void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture)
    const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); // this function implementation will call autorelease.So it's not thread-safe.
    CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath);

    addSpriteFramesWithDictionary(dict, pobTexture);


If you want to load resources in an sub-thread. Look into TextureCacheTest. Thanks.


but i want to ask the function loadGame must be define static ?


Not have to. static means that this function can only be used in this cpp file.


James Chen
can you give me a example about pthread~\ thanks~
if i define
void * loadGame(void *ptr)

how to write pthread_create~~


Maybe you should search some infomations from google. Also, you can read CCTextureCache.cpp. Hope this will help you.