当前位置 : 主页 > 手机开发 > harmonyos >

cocos2dx之CCDirector

来源:互联网 收集:自由互联 发布时间:2023-08-25
CCDirector中的三个比较重要的成员方法: void runWithScene(CCScene *pScene);// 运行pScene整个场景 /**Suspends the execution of the running scene, pushing it on the stack of suspended scenes. * The new scene will be execute


CCDirector中的三个比较重要的成员方法:

void runWithScene(CCScene *pScene);// 运行pScene整个场景

    /**Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
     * The new scene will be executed.
     * Try to avoid big stacks of pushed scenes to reduce memory allocation. 
     * ONLY call it if there is a running scene.
     */
    void pushScene(CCScene *pScene);// 将当前运行的场景进行压栈,用将要执行的场景来替换它

    /**Pops out a scene from the queue.
     * This scene will replace the running one.
     * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
     * ONLY call it if there is a running scene.
     */
    void popScene(void);//将栈中的第一个scene出栈,释放掉它



值的注意的是:以上三种切换场景的方法(replaceScene、pushScene、popScene)均是先将待切换的场景完全加载完毕后,才将当前运行的场景释放掉。所以,在新场景恰好完全加载完毕的瞬间,系统中同时存在着两个场景,这将是对内存的一个考验,若不注意的话,切换场景可能会造成内存不足。


我们继续看例子:

void MyChipmunkAccelScene::runMyScene() {
    MyChipmunkAccelLayer *layer = new MyChipmunkAccelLayer;
    layer->initLayer();
    this->addChild(layer, 1);
    CCScene *runscene = CCDirector::sharedDirector()->getRunningScene();
    CCDirector::sharedDirector()->pushScene(runscene);//先将当前的runscene放入到堆栈中
    CCDirector::sharedDirector()->popScene();//释放掉它

    CCDirector::sharedDirector()->replaceScene(this);


    layer->autorelease();

}




例子:

void MyChipmunkAccelScene::runMyScene() {
    MyChipmunkAccelLayer *layer = new MyChipmunkAccelLayer;
    layer->initLayer();
    this->addChild(layer, 1);
   // CCScene *runscene = CCDirector::sharedDirector()->getRunningScene();
    CCDirector::sharedDirector()->pushScene(this);
    this->retain();// 因为cocos2dx采用的是计数进行内存管理,

    layer->autorelease();

}



MyChipmunkAccelScene::~MyChipmunkAccelScene() {
    CCDirector::sharedDirector()->popScene();// 在使用完的时候直接pop掉
}


其实这里也可以不用这样写,看到了源码,在结束当前的scene的时候,会结束当前scene中的scene:

ccArrayRemoveAllObjects



通过:

m_pTargetedHandlers->removeAllObjects();





上一篇:cocos2dx之cCCSpawn
下一篇:没有了
网友评论