C#学习日志 day9 -------------- cocos2d-x 3.0 粒子效果简介
cocos2d是一款实用的游戏引擎,其可以在windows phone中应用,但是比较差强人意的是其代码只能用c++来写。
因为我们的wp应用我负责粒子效果的实现,所以这里我介绍一下各种粒子效果。
cocos2d中自带了十种粒子效果。可以说,一般的效果我们都可以用cocos2d自带的粒子效果实现。即使不能我们也可以通过对其自带粒子效果进行一些属性的调整从而实现我们想要的效果。
这里先贴出十种粒子效果的实现代码
ParticleRain *snow = ParticleRain::create();//雨的效果 snow->setPosition(Point(400, 300)); snow->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("black.png"), Rect(0, 0, 10, 10)); this->addChild(snow); ParticleExplosion *exp = ParticleExplosion::create();//爆炸效果 exp->setPosition(Point(300, 300)); exp->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(exp); ParticleFire *fire = ParticleFire::create();//火焰效果 fire->setPosition(Point(200, 300)); fire->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(fire); ParticleSun *sun = ParticleSun::create();//太阳效果。火焰效果的重力设置为0就是了 sun->setPosition(Point(100, 300)); sun->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(sun); ParticleGalaxy *ga = ParticleGalaxy::create();//星系效果, ga->setPosition(Point(500, 300)); ga->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(ga); ParticleSmoke *sm = ParticleSmoke::create();//烟的效果,速度比火焰慢 sm->setPosition(Point(600, 300)); sm->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(sm); ParticleMeteor *pm = ParticleMeteor::create();//流行效果 pm->setPosition(Point(100, 200)); pm->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(pm); ParticleFlower *pb = ParticleFlower::create();//花的效果,个人感觉特别乱 pb->setPosition(Point(200, 200)); pb->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(pb); ParticleSpiral *ps = ParticleSpiral::create();//螺旋形 ps->setPosition(Point(300, 200)); ps->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(ps); ParticleFireworks *pf = ParticleFireworks::create();//烟花效果 pf->setPosition(Point(400, 200)); pf->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 10, 10)); this->addChild(pf);
简单给出效果可以清楚的看出。都很丑。
但是这是因为使用图片未经选择和没有经过调整。
这里给出几个关键的属性,通过调整可以使得粒子效果很酷炫。
ParticleMeteor *pm = ParticleMeteor::create();
setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("yellow.png"), Rect(0, 0, 5, 5));//粒子引用的图片以及rect的大小 pm->setPosition(Point(x, y));//出现的位置 pm->setGravity(Point(-2, 0));//重力设置 pm->setStartSize(8.0f);//开始时粒子的size pm->setTotalParticles(550);//设置粒子数 pm->setDuration(0.5);//粒子持续时间,以秒为单位
点击时这个火焰的效果会持续0.5秒,然后消失,再次触摸继续出现0.5秒。