我用cocos2dx pthread.h 这里面的方法创建了个线程,线程里面就创建一个精灵(sprite)将创建好的返回给Layer这个层让它加进去显示,不过现出不出来,直接在layer里面加就可以,我这边是通过一个指针保存创建layer对象指针后通过线程调用静态方法将精灵加到层里面!求解是不是思路错了?
还有cocos2dx添加的对象只能是继承内部类的类才可以将创建好的对象加进去并显示?
下面代码
#include “pthreads.h”
#include “pthread.h”
#include
#include “HelloWorldScene.h”
using namespace cocos2d;
int ThreadUtils::counts = 0;
void* ThreadUtils::start()
{
char* p = new char;
++counts;
id = counts;
sprintf_s(p,20,“hello %d”,id);
if( pthread_create(&pid,NULL,start_thread,(void *)p) != 0 )
{
return NULL;
}
return p;
}
void* ThreadUtils::start_thread(void * arg)
{
//CCLog("%s…",(char*)arg);
//随机数产生
srand(unsigned(time(0)));
//CCLog("num : %f
",num);
for(int i=0;i<=10;i++)
{
CCLog("new thread : %s
",(char*)arg);
}
//创建一个sprit
//CCSprite* sprit = CCSprite("g.png");//CCSprite::create("g.png");
//创建一个文本layer多线程加入到场景里面
CCLabelTTF* pLabel = CCLabelTTF::create("1243................", "Arial", 29);
if(pLabel == NULL)
{
CCLog("pLabel create false
");
return NULL;
}
pLabel->runAction( CCRepeatForever::create( CCRotateBy::create(2.f,360.f) ) );
pLabel->retain();
HelloWorld::addNewNode(pLabel);
//HelloWorld::getLayer()->addChild(pLabel);
//HelloWorld::getLayer()->getChildByTag(99)->setPosition(ccp(0.f,0.f));
//delete(arg);
CCLog("counts : %d
",pLabel->retainCount());
return arg;
}
layer里面的
void HelloWorld::addNewNode(CCNode* node)
{
srand(unsigned(time(0)));
//CCScene* nowsence = CCDirector::sharedDirector()->getRunningScene();
CCSize size = CCDirector::sharedDirector()->getWinSize();
node->setPosition(ccp(50.f, 50.f) );
//CCLabelTTF* pLabel = CCLabelTTF::create("new ..........", "Arial", 20);
//if(HelloWorld::p_nowLayer != NULL)
node->setVisible( true );
HelloWorld::p_nowLayer ->addChild(node,9);
//nowsence->addChild(node);
}