看了引用计数之后 那时好像懂了 今天突然想起一个问题:
classCC_DLLScene:publicNode
{
public:
/** creates a new Scene object */
staticScene*create();
{
Scene *Scene::create()
{
Scene *ret =newScene();
if(ret && ret->init())
{
ret->autorelease();
returnret;
}
else
{
CC_SAFE_DELETE(ret);
returnnullptr;
}
}
}
}
intAPIENTRY_tWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
intnCmdshow)
{
returnApplication::getInstance()->run();
其中run(){
while(!glview->windowShouldClose())
{
QueryPerformanceCounter(&nNow);
if(nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
其中mainLoop();
{
drawScene();
PoolManager::getInstance()->getCurrentPool()->clear();
for(constauto&obj : _managedobjectArray)
{obj->release();}
}
glview->pollEvents();
}
else
Sleep(0);
}
}
}
}
我们看到蓝色代码那里,PoolManager::getInstance()->getCurrentPool()->clear();
Scene加入autorelease了 node继承的都加入了 Sprite也加入了 我们知道ref的构造函数是把引用计数自动变成1,那这里每一帧的绘画会把对象池里面的scene node sprite调用一次release 如果没有其他retain也就是变成0要回收了,但是不是这样的,实际还是可以显示出来的
1、scene的runWithScene
boolAppDelegate::applicationDidFinishLaunching() {
autodirector =Director::getInstance();
autoglview = director->getopenGLView();
if(!glview) {
glview =GLView::create("WeChat Airplane");
glview->setFrameSize(600,800);
director->setopenGLView(glview);
}
director->setdisplayStats(true);
director->setAnimationInterval(1.0 / 60);
glview->setDesignResolutionSize(480,800,ResolutionPolicy::SHOW_ALL);
this->setResourceSearchResolution();
autoscene =WelcomeScene::create();
director->runWithScene(scene);
runWithScene(scene){
pushScene(scene);
{
_scenesStack.pushBack(scene);
{
_data.push_back(object);
object->retain();
}
}
}
returntrue;
}
那么就不会被回收了 那所有的node其实都是添加到scene里面的,也就不会因为release Scene而导致node减1
2、继承node的sprite这些等等
我们知道都要addChild的 layer也好 node也好 sprite也好 都是要加进去的
voidNode::addChild(Node *child)
{
CCASSERT( child !=nullptr,"Argument must be non-nil");
this->addChild(child,child->_localZOrder,child->_tag);
{
voidNode::insertChild(Node*child,intz)
{_transformUpdated =true;_reorderChildDirty =true;_children.pushBack(child);
voidpushBack(Tobject){CCASSERT(object !=nullptr,"The object should not be nullptr");_data.push_back(object);object->retain();}child->_setLocalZOrder(z);}