cocos2d-x学习之粒子系统

cocos2d-x里的tests工程内容丰富,其中最炫的一个test就是ParticleTest,此test包含43个不同的粒子渲染效果,很好的诠释了cocos2d-x的粒子系统。

在controller.cpp中找到回调函数menuCallback,这个函数在touch主界面的list条目时调用,进入对应的test实例。代码:

    // get the userdata, it's the index of the menu item clicked
    CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
    int nIdx = pMenuItem->getZOrder() - 10000;

    // create the test scene and run it
    TestScene* pScene = CreateTestScene(nIdx);
    if (pScene)
    {
        pScene->runThisTest();
        pScene->release();
    }

得到touch条目的索引,然后创建相应的场景,最后执行runThisTest。

看CreateTestScene(nIdx)函数,其中用一个switch case语句来根据索引创建响应的test场景。

找到ParticleTestScene(),这个就是粒子系统的场景。

因为上面创建场景后是执行场景的runThisTest()函数,所以直接看ParticleTestScene的runThisTest。

    addChild(nextParticleAction());

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

代码很简单,只有两句。添加布景,导演切换场景。

看nextParticleAction()函数,这个函数创建不同的布景。

    sceneIdx++;
    sceneIdx = sceneIdx % MAX_LAYER;

    CCLayer* pLayer = createParticleLayer(sceneIdx);
    pLayer->autorelease();

    return pLayer;
可以看到,这个粒子test包含43个实例。用switch-case根据索引创建。

先到这里,后续挨个看各个粒子实例的代码。


你可能感兴趣的:(list,menu,layer)