cocos 2dx

Cocos2d-X 雪花效果

1.首先定义一个 Snowflake 效果的方法
Snowflake.h

    static void createParticleSnowRing(cocos2d::CCPoint position, Engine::String image ,int radia, CCNode* parentNode);

Snowflake.cpp

    void ParticleEffects::createParticleSnowRing(cocos2d::CCPoint position, Engine::String image ,int radia, CCNode* parentNode)   {
    CCParticleSnow * particle = CCParticleSnow::node();
    //自定义雪花图片
    particle->setTexture(CCTextureCache::sharedTextureCache()->addImage(image.c_str()));
    //粒子总数
    particle->setTotalParticles(400);
    //下雪时间秒
    particle->setDuration(30);
    //雪花开始大小    
    particle->setStartSize(10);
    //雪花大小浮动
    particle->setStartSizeVar(5);
    //雪花结束大小
    particle->setEndSize(5);
    particle->setEndSizeVar(0);
    //初始速度    
    particle->setSpeed(200);
    particle->setSpeedVar(100); 
    //下降速度 y负数表示下落 x负数表示向西吹
    particle->setGravity(ccp(-20,-70));
    //每个雪花生命时间
    particle->setLife(20.0f);
    particle->setLifeVar(2.0f); 
    //            particle->setPosition(ccp(240, 320));
    //释放雪花速度 particle.emissionRate = particle.totalParticles/particle.life;
    particle->setEmissionRate(400/4);  
    CCSize a = CCDirector::sharedDirector()->getWinSize();
    //动画完成后自动从上层layer中移除粒子
    particle->setAutoRemoveOnFinish(true);
    //释放粒子位置 如果你想在调用的时候设置位置,那么可以注视下面的代码
    parentNode->addChild(particle,3);
}    

2.在你想调用的地方写下面的代码即可:

    Engine::String image = Engine::StringUtil::format("Snowflake%d.png",arc4random()% 7 + 1 );
    CCSizes = CCDirector::sharedDirector()->getWinSize();
    int x = s.width;
    int y = s.height;
    CCPoint position = ccp(arc4random() % x,arc4random() % y);
    this-> createExplodingRing(position, image, arc4random()%30+50, this);

3.如果你单独写一个类的话,这么调用

    EffectsScene= newParticleEffects();
    EffectsScene-> createExplodingRing(position, image, arc4random()%30+50, this);

4.直接用在用

    CCParticleSnow* particle = CCParticleSnow::node();
    particle->setTexture(CCTextureCache::sharedTextureCache()->addImage("snowflakes.png"));
    particle->setTotalParticles(400);
    particle->setDuration(30); 
    particle->setStartSize(10);
    particle->setStartSizeVar(5);
    particle->setEndSize(5);
    particle->setEndSizeVar(0);  
    particle->setSpeed(200);
    particle->setSpeedVar(100); 
    particle->setGravity(ccp(-20,-70));
    particle->setLife(20.0f);
    particle->setLifeVar(2.0f); 
    particle->setEmissionRate(400/4);  
    CCSizea = CCDirector::sharedDirector()->getWinSize();
    particle->setAutoRemoveOnFinish(true);
    particle->setPosition(CCPoint(a.width/2,a.height+10)); 
    particle->setPosVar(ccp(a.width/2+50,0));
    this->addChild(particle,20);

5.一些效果:

    -- CCParticleExplosion         (爆炸粒子效果)
    -- CCParticleFireworks       (烟花粒子效果)
    -- CCParticleFire                   (火焰粒子效果)
    -- CCParticleFlower              (花束粒子效果)
    -- CCParticleGalaxy             (星系粒子效果)
    -- CCParticleMeteor            (流星粒子效果)
    -- CCParticleSpiral              (漩涡粒子效果)
    -- CCParticleSnow               (雪粒子效果)
    -- CCParticleSmoke             (烟粒子效果)
    -- CCParticleSun                  (太阳粒子效果)
    -- CCParticleRain                 (雨粒子效果)

这个是摘的别人那边 看着不错 记下来 下次可以方便查看

你可能感兴趣的:(CCParticleSnow)